[英]Multi-dimensional array in C++
Is there an available implementation of multi-dimensional array in C++ where the number of dimensions are specified at runtime? 在C ++中是否存在可用的多维数组实现,其中在运行时指定了维数? Basically, I am looking for something like this:
基本上,我正在寻找这样的事情:
// create a 4 dimensional array
int num_dim = 4;
int sizes[4] = {3, 2, 4, 5}; // size for each dimension
MultiArray<int> A( num_dim, sizes );
// accessing a element in the array
int index[4] = {2, 2, 1, 0};
A[index] = 3;
Edit: Notice that I don't want to use either std::vector<std::vector<...>>
or boost::multi_array
because I do not know the number of dimensions before hand. 编辑:请注意,我不想使用
std::vector<std::vector<...>>
或boost::multi_array
因为我不知道之前的尺寸数。
Yes the implementation exists in boost 是的,实现存在于提升中
http://www.boost.org/doc/libs/1_55_0/libs/multi_array/doc/user.html http://www.boost.org/doc/libs/1_55_0/libs/multi_array/doc/user.html
typedef boost::multi_array<double, 3> array_type;
typedef array_type::index index;
array_type A(boost::extents[3][4][2]);
Notice that I don't want to use either
std::vector<std::vector<...>>
orboost::multi_array
because I do not know the number of dimensions before hand.请注意,我不想使用
std::vector<std::vector<...>>
或boost::multi_array
因为我不知道之前的尺寸数。
Use of std::vector<std::vector<...>>
is most appropriate for your situation. 使用
std::vector<std::vector<...>>
最适合您的情况。
int m;
int n;
// Read the dimensions of the 2D array.
std::cin >> m >> n;
// Create a 2D array of size m x n using `std::vector`.
std::vector<std::vector<int>> array(m, std::vector<int>(n));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.