c++ - C++ 中的多维可变大小数组

标签 c++ arrays

你好,我想做这样的事情:

int op(string s1, string s2){
    int x = s1.size();
    int y = s2.size();
    int matrix = new int[x][y]
    /* do stuff with matrix */
}

出于某种原因,我收到以下错误:

SuperString.cpp(69) : error C2540: non-constant expression as array bound
SuperString.cpp(69) : error C2440: 'initializing' : cannot convert from 'int (*)[1]' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
SuperString.cpp(71) : error C2109: subscript requires array or pointer type

谢谢!

最佳答案

这里总结了如何使用各种技术在 C++ 中构建二维数组。

静态二维矩阵:

const size_t N = 25; // the dimension of the matrix

int matrix[N][N]; // N must be known at compile-time.
// you can't change the size of N afterwards

for(size_t i = 0; i < N; ++i)
{
    for(size_t j = 0; j < N; ++j)
    {
        matrix[i][j] = /* random value! */;
    }
}

动态二维矩阵:

const size_t N = 25; // the dimension of the matrix
int** matrix = new int*[N]; // each element is a pointer to an array.

for(size_t i = 0; i < N; ++i)
    matrix[i] = new int[N]; // build rows

for(size_t i = 0; i < N; ++i)
{
    for(size_t j = 0; j < N; ++j)
    {
        matrix[i][j] = /* random value! */;
    }
}

// DON'T FORGET TO DELETE THE MATRIX!
for(size_t i = 0; i < N; ++i)
    delete matrix[i];

delete matrix;

使用 std::vector 的矩阵:

// Note: This has some additional overhead
// This overhead would be eliminated once C++0x becomes main-stream ;)
// I am talking about r-value references specifically.
typedef vector< vector<int> > Matrix;
typedef vector<int> Row;

const size_t N = 25; // the dimension of the matrix
Matrix matrix;

for(size_t i = 0; i < N; ++i)
{
    Row row(N);

    for(size_t j = 0; j < N; ++j)
    {
        row[j] = /* random value! */;
    }

    matrix.push_back(row); // push each row after you fill it
}

// Once you fill the matrix, you can use it like native arrays
for(size_t i = 0; i < N; ++i)
{
    for(size_t j = 0; j < N; ++j)
    {
        cout << matrix[i][j] << " ";
    }

    cout << endl;
}

3d 矩阵使用 boost::multi_array ( taken from boost multi_array docs ):

// Note that this is much more efficient than using std::vector!
int 
main () {
  // Create a 3D array that is 3 x 4 x 2
  typedef boost::multi_array<double, 3> array_type;
  typedef array_type::index index;
  array_type A(boost::extents[3][4][2]);

  // Assign values to the elements
  int values = 0;
  for(index i = 0; i != 3; ++i) 
    for(index j = 0; j != 4; ++j)
      for(index k = 0; k != 2; ++k)
        A[i][j][k] = values++;

  // Verify values
  int verify = 0;
  for(index i = 0; i != 3; ++i) 
    for(index j = 0; j != 4; ++j)
      for(index k = 0; k != 2; ++k)
        assert(A[i][j][k] == verify++);

  return 0;
}

关于c++ - C++ 中的多维可变大小数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1946830/

相关文章:

java - 从 ByteBuffer 中提取 Longs (Java/Scala)

c++ - 指向二维数组的指针

c++ - 使用重载的 operator= 不编译

c++ - nullptr_t驻留在哪里?

c++ - RInside 编译 : Make Targets

c - 使用结构的矩阵

Javascript for 循环不循环遍历数组

c++ - 使用 libjpeg 解码期间的无损 JPEG 旋转

c++ - 静态变量链接错误

ios - 添加到数组时无法推断通用参数 'Element'