具有可变大小行的 C++ 2 维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11535384/
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
C++ 2 dimensional array with variable size rows
提问by user1484717
How can you create a 2D array,say, arr[][]
with 5 rows and each row has a variable number of columns in it?
你怎么能创建一个二维数组,比如说,arr[][]
有 5 行,每行都有可变数量的列?
possibly arr[5][]
with 1st row arr[0][]
with 4 columns
可能arr[5][]
第一行arr[0][]
有 4 列
2nd row arr[1][]
with 5 columns and so on?
第二行arr[1][]
有 5 列等等?
I wouldn't mind a STL vector solution but I don't know vectors very well yet.
我不介意 STL 向量解决方案,但我还不太了解向量。
回答by SingerOfTheFall
With C++11, you can do it easily with vectors (line breakes added for readability):
使用 C++11,您可以使用向量轻松完成(添加换行符以提高可读性):
std::vector< std::vector <int > > arr = {
{1,2,3},
{4,5},
{6,7,8,9,0}
};
If you don't have a C++11 compiler, it works the exact same way, but you will not be able to initialize them as easy. You can set elements individually:
如果您没有 C++11 编译器,它的工作方式完全相同,但您将无法轻松初始化它们。您可以单独设置元素:
std::vector< std::vector <int > > arr;//vector of vectors. Think of each element as of a "row"
std::vector<int> sub;//a temporary "row"
sub.push_back(1);
sub.push_back(2);
arr.push_back(sub);//Adding a "row" to the vector
sub.clear();//Making another one
sub.push_back(1);
sub.push_back(12);
sub.push_back(54);
arr.push_back(sub);//Adding another "row" to the vector
Or you can initialize each "row" with an ordinary array:
或者您可以使用普通数组初始化每个“行”:
std::vector< std::vector <int > > arr;
static const int arr[] = {1,2,3,4};//A "row" as an ordinary array
vector<int> vec (arr, arr + sizeof(arr) / sizeof(arr[0]) ); //Setting a "Row" as a vector
arr.push_back(vec);//Adding the "row" to the vector of vectors.
It's not exactly possible to do what you want with ordinary arrays, since when you make an array[X][Y]
, it automaticaly is an X*Y
matrix. You could, however, use an array of pointers:
用普通数组做你想做的事情是不可能的,因为当你做一个时array[X][Y]
,它自动地是一个X*Y
矩阵。但是,您可以使用指针数组:
int * array[3];
//also possible: int ** array = new int*[3]; but don't forget to delete it afterwards.
int sub1[3] = {1,2,3};
int sub2[2] = {1,2};
int sub3[4] = {1,2,3,4};
array[0] = sub1;
array[1] = sub2;
array[2] = sub3;
and access elements with array[X][Y]
. However, the vector solution is much better overall.
和访问元素array[X][Y]
。但是,矢量解决方案总体上要好得多。
回答by Lyubomir Vasilev
You can do it like this (assuming an array of int
elements):
你可以这样做(假设一个int
元素数组):
int** arr = new int*[5];
for(size_t i = 0; i < 5; ++i)
{
arr[i] = new int[4];
}
and this gives you a two-dimensional dynamically allocated array of 5 by 4. You can then use it like this: arr[i][j] = 15;
这为您提供了一个 5 x 4 的二维动态分配数组。然后您可以像这样使用它: arr[i][j] = 15;
Do not forget to de-allocate the memory after you are done using the array:
使用完数组后,不要忘记取消分配内存:
for(size_t i = 0; i < 5; ++i)
{
delete[] arr[i];
}
delete[] arr;
I would recommend using std::vector
, however. You can see the other answers for reference.
std::vector
但是,我建议使用。您可以查看其他答案以供参考。
回答by reza moradi
A way for different row size
不同行大小的方法
#include <iostream>
#include <string>
int main()
{
int test1[]={1,2,3};
int test2[]={4,5};
int *test[]={test1,test2};
std::cout << test[0][1];//2
std::cout << test[1][1];//5
}
回答by ForEveR
So, 2D arrays is std::vector<std::vector<T>>
, where T
is type. Also, mb std::array<std::vector<int>, 5>
. Or write your own array class.
所以,二维数组是std::vector<std::vector<T>>
,其中T
是类型。另外,mb std::array<std::vector<int>, 5>
。或者编写自己的数组类。