C++ 调整多维向量的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4311953/
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 15:05:47  来源:igfitidea点击:

resizing multidimensional vector

c++stl

提问by qutron

How to resize multidimensional vector such as:

如何调整多维向量的大小,例如:

  vector <vector <vector <custom_type> > > array; 

For example, I need array[3][5][10]?

例如,我需要数组[3][5][10]?

采纳答案by Tony Delroy

You should resize all the nested vectors one by one. Use nested forloops or recursion.

您应该vector一一调整所有嵌套的s 的大小。使用嵌套for循环或递归。

回答by Matt

array.resize(3,vector<vector<custom_type> >(5,vector<custom_type>(10)));

回答by qutron

I did it))

我做到了))

array.resize(3);
for (int i = 0; i < 3; i++)
{
    array[i].resize(5);
    for (int j = 0; j < 5; j++)
    {
       array[i][j].resize(10);
    }
}

回答by davka

see also Boost.MultiArray

另见Boost.MultiArray

Boost.MultiArray provides a generic N-dimensional array concept definition and common implementations of that interface.

Boost.MultiArray provides a generic N-dimensional array concept definition and common implementations of that interface.

回答by stefaanv

I would make a custom container containing a vector of vectors (of ... per dimension) and resize with resize-functions per dimension. This way you can put the invariant of equal size per dimension in one place. The actual resizing can then be done in a loop according to dimension.
There will be a bit of work involved in making public what needs to be accessed (operator[],...)

我会制作一个包含向量向量(每个维度的向量)的自定义容器,并使用每个维度的调整大小函数调整大小。通过这种方式,您可以将每个维度相同大小的不变量放在一个地方。然后可以根据维度在循环中完成实际的调整大小。
公开需要访问的内容(operator[],...)