简体   繁体   English

C ++中具有可变维度的二维数组

[英]2D array in C++ with variable dimentions

So I am teaching myself C++ and I have a doubt about arrays.所以我在自学 C++,我对数组有疑问。 I know that if I declare a 2D array like:我知道,如果我声明一个二维数组,如:

char board[8][8];

I create a 2D array with a height and a width of 8. But, I was wondering if there is a way to set those dimensions to variables so the user can input the height and width they want, I tried this in C++ and visual studio wasn't happy.我创建了一个高度和宽度为 8 的二维数组。但是,我想知道是否有办法将这些尺寸设置为变量,以便用户可以输入他们想要的高度和宽度,我在 C++ 和 Visual Studio 中尝试了这个不开心。

int rowSize = 0;  
int colSize = 0;
cin >> rowSize >> colSize;
char board[rowSize][colSize];

can anyone help me?谁能帮我?

Since this is tagged with c++ , perhaps std::vector is for you.由于这是用c++标记的,因此std::vector可能适合您。 It's much like an array, but more convenient and well suited for this purpose.它很像一个数组,但更方便,更适合这个目的。 You can simply initialize a 2D Array of nested vectors with this initializer call (thanks to Bob__ for this much more simple solution):您可以使用此初始化程序调用简单地初始化嵌套向量的 2D 数组(感谢 Bob__ 提供了这个更简单的解决方案):

int n = 5;
int m = 10;
std::vector<std::vector<char>> board (n, std::vector<char>(m, 'a'));

This creates the board and initializes all fields to 'a' , per example.这将创建板并将所有字段初始化为'a' ,每个示例。 You can access and manipulate the data using the same syntax as with C style arrays:您可以使用与 C 样式数组相同的语法访问和操作数据:

char x = board[4][2];
board[3][3] = 'o';

Last but not least, there are lots of convenient features and functions that allow you to do things like copying it much more easily than with C-style arrays.最后但并非最不重要的一点是,有许多方便的特性和函数使您可以比使用 C 样式数组更轻松地执行复制等操作。 Check out the documentation of std::string here .在此处查看std::string的文档。

If you want to allocate such matrices, use a vector and index it with i + j * rowSize .如果要分配此类矩阵,请使用向量并使用i + j * rowSize

The other construction is not c++ compliant, it's an gcc extension from C99 (Variable Length Arrays), and their equivalent (more or less) are a vector or a unique pointer to an array.另一个结构不符合 c++,它是 C99(可变长度数组)的 gcc 扩展,它们的等价物(或多或少)是向量或指向数组的唯一指针。

If you really want to do this you could do the follwing:如果您真的想这样做,您可以执行以下操作:

int* myPointer = nullptr;   // Pointer to int, initialize to nothing.
int sizeOfArray;           // Size needed for array
std::cin >> sizeOfArray;        // At runtime get the size of the array
myPointer = new int[sizeOfArray];  // Allocate array of specified size and save ptr in a.
for (int i = 0; i < sizeOfArray; i++) {
    myPointer[i] = 0;    // Initialize all elements to zero.
}
delete[] myPointer;  // When done, free memory pointed to by myPointer.
myPointer = nullptr;     // Clear a to prevent using invalid memory reference.

Although I would strongly recommend you use a vector instead.尽管我强烈建议您改用矢量

No you cannot in conformant C++, because the standard does not define Variable Length Arrays (*).不,你不能在符合 C++ 中,因为标准没有定义可变长度数组 (*)。

If you do not need consecutive allocation (a true 2D array underlying storage), you can use a vector of vectors.如果不需要连续分配(真正的二维数组底层存储),可以使用vector的vector。 This is generally the most simple and idiomatic way:这通常是最简单和惯用的方式:

std::vector<std::vector<char>>(8, std::vector<char>(8, '\0'));

If you can accept a functionnal type access, you can build a custom container with an underlying 1D vector of size 64 (8*8), and an accessor method returning a reference.如果您可以接受函数类型访问,则可以使用大小为 64 (8*8) 的底层一维向量和返回引用的访问器方法构建自定义容器。

If you want to mimic 2D container with an underlying true 2D array, then you are in trouble.如果你想用一个底层真正的 2D 数组来模拟 2D 容器,那么你就有麻烦了。 I have tried to build a generic multi-dimensional container (code in Code Review ), and realized that you cannot correctly implement random access iterators on non standard containers...我试图构建一个通用的多维容器(代码审查中的代码),并意识到您无法在非标准容器上正确实现随机访问迭代器......


(*) of course, gcc gladly accept VLA in C++ as a documented extension to the language... (*) 当然,gcc 很乐意接受 C++ 中的 VLA 作为该语言的文档扩展......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM