考虑 2 种类型的数组声明:
T x [rows * cols];  // type 1
T y [rows][cols];   // type 2
我通常使用第一种类型(类型 1),然后我知道使用 x[row * cols + col] 进行索引
但是,如果我想将二维数组复制到模拟二维数组的一维数组中,即:复制 type2 -> type1。如果保证这些在内存中的布局方式相同,我可以只对另一个进行 memcpy 吗?目前我有一个这样的循环,但如果内存在两者中的布局相同,我想我可以做一个 memcpy。考虑下面的公共构造函数。
public:
  // construct a matrix from a 2d array
  template <unsigned int N, unsigned int M>
  Matrix ( T (&twoDArray)[N][M]  )  : rows_(N), cols_(M), matrixData_(new T[rows_*cols_]) 
  {
    // is there a refactor here? Maybe to memcpy?
    for ( unsigned int i = 0; i < rows_ ; ++i )
    {
      for ( unsigned int j = 0; j < cols_ ; ++j )
      {
        matrixData_[ i * cols_ + j ] = twoDArray[i][j];
      }
    }
  }
  private:
  unsigned int rows_;
  unsigned int cols_;
  T* matrixData_;