【问题标题】:Matrix class with std::array带有 std::array 的矩阵类
【发布时间】:2015-05-09 09:12:31
【问题描述】:

我想构建一个简单的矩阵类。我的头文件的相关部分如下所示:

template <typename T>
class matrix
{
private:
  unsigned int nrows;
  unsigned int ncols;
  std::array<std::array<T, ncols>, nrows> mat;

public:
  matrix();

  unsigned int getCols() const;
  unsigned int getRows() const;

};

这里的问题是二维数组(称为mat)需要行数和列数。显然,这不起作用,但我不知道如何解决这个问题。

我的源文件:

template <typename T>
matrix<T>::matrix() : nrows(0), ncols(0) {}

template <typename T>
unsigned int matrix<T>::getCols() const {
  return ncols;
}

template <typename T>
unsigned int matrix<T>::getRows() const {
  return nrows;
}

矩阵的初始化应该是这样的:

matrix<double> my_matrix;

【问题讨论】:

  • 使用std::vector&lt;std::vector&gt; std::array&lt;std::array&gt; 尺寸必须在编译时知道。

标签: c++ arrays class matrix


【解决方案1】:

sizearray 不能有可变大小的参数。因此,matrix 类必须有另外两个模板参数。

template <typename T, size_t ROWS, size_t COLS>
class matrix
...

【讨论】:

  • 感谢您的提示,由于需要 push_back 等,我现在已经使用 std::vector 实现了该类。
猜你喜欢
  • 1970-01-01
  • 2020-02-07
  • 2019-08-14
  • 1970-01-01
  • 2012-03-16
  • 1970-01-01
  • 2017-03-31
  • 1970-01-01
  • 2020-07-31
相关资源
最近更新 更多