在 C++ 中声明可变长度二维数组的正确方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8294102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:19:40  来源:igfitidea点击:

Proper way to declare a variable length two dimensional array in C++

c++arraysmultidimensional-arraydynamic-sizing

提问by Christian

I would like to get a two dimensional int array arrthat I can access via arr[i][j].

我想获得一个arr可以通过 arr[i][j] 访问的二维 int 数组。

As far as I understand I could declare int arr[10][15];to get such an array. In my case the size is however variable and as far as I understand this syntax doesn't work if the size of the array isn't hardcoded but I use a variable like int arr[sizeX][sizeY].

据我所知,我可以声明int arr[10][15];获得这样的数组。在我的情况下,大小是可变的,据我所知,如果数组的大小不是硬编码的,则此语法不起作用,但我使用像int arr[sizeX][sizeY].

What's the best workaround?

最好的解决方法是什么?

回答by Some programmer dude

If you don't want to use a std::vectorof vectors (or the new C++11 std::array) then you have to allocate all sub-arrays manually:

如果您不想使用 a std::vectorof 向量(或新的 C++11 std::array),则必须手动分配所有子数组:

int **arr = new int* [sizeX];
for (int i = 0; i < sizeX; i++)
    arr[i] = new int[sizeY];

And of course don't forget to delete[]all when done.

当然,完成后不要忘记delete[]所有人。

回答by BruceAdi

c/c++ does not support multidimensional array. But it does support array of array:

c/c++ 不支持多维数组。但它确实支持数组数组:

    //simulate 2-dimension array with 1-dimension array
    {
        int x = 20;
        int y = 40;
        int * ar = new int(x*y);
        int idx_x =9;
        int idx_y=12;
        ar[idx_x + idx_y * x] = 23;
    }
    //simulate 2-dimension array with array of array
    {
        int x = 20;
        int y = 40;
        int**ar = new int*[y];
        for(int i = 0; i< y; ++i)
        {
            ar[i] = new int[x];
        }
        ar[9][12] = 0;      
    }

回答by cpp

As fefe said you can use vector of vectors, e. g. :

正如fefe所说,您可以使用向量向量,例如:

#include<iostream>
#include<vector>
using namespace std;

int main()
{
        vector< vector<int> > vec;
        vector<int> row1;
        row1.push_back(1);
        row1.push_back(2);
        vector<int> row2;
        row2.push_back(3);
        row2.push_back(4);

        vec.push_back(row1);
        vec.push_back(row2);

        for( int ix = 0; ix < 2; ++ix)
            for( int jx = 0; jx < 2; ++jx)
                cout << "[" << ix << ", " << jx << "] = " << vec[ix][jx] << endl;

}

回答by Chris Parton

If you're looking for the solution in C, see this Stack Overflow thread: How do I work with dynamic multi-dimensional arrays in C?.

如果您正在寻找 C 中的解决方案,请参阅此堆栈溢出线程: 如何在 C 中使用动态多维数组?.

If C++ is okay, then you can create a 2D vector, i.e. a vector of vectors. See http://www.cplusplus.com/forum/general/833/.

如果C++没问题,那么你可以创建一个二维向量,即向量的向量。请参阅http://www.cplusplus.com/forum/general/833/

回答by Daniel Daranas

You can't, with array syntax. The language requires that you use compile time constants to create arrays.

你不能,用数组语法。该语言要求您使用编译时常量来创建数组。

回答by fefe

C++ does not have variable length arrays. You can consider using vector<vector<int> >. It can be also accessed using arr[i][j].

C++ 没有变长数组。您可以考虑使用vector<vector<int> >. 也可以使用arr[i][j].