试图在 C++ 中创建一个 3 维向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9812411/
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
Trying to create a 3 dimensional vector in c++
提问by Mete
So, im trying to create a 3 dimensional 5x3x2 vector, using the vector lib and saving the number 4 in every node.
所以,我试图创建一个 3 维 5x3x2 向量,使用向量库并在每个节点中保存数字 4。
Thats what im trying:
这就是我正在尝试的:
vector<vector<vector<int> > > vec (5,vector <int>(3,vector <int>(2,4)));
for a bi dimensional 5x8 saving the int 6 in every node, this works:
对于在每个节点中保存 int 6 的二维 5x8,这是有效的:
vector<vector<int> > vec (5,vector <int>(8,6));
回答by dasblinkenlight
You almost got it right -- the second nested vector
should be vector<vector<int> >
, not just a vector<int>
:
你几乎猜对了——第二个嵌套vector
应该是vector<vector<int> >
,而不仅仅是一个vector<int>
:
vector<vector<vector<int> > > vec (5,vector<vector<int> >(3,vector <int>(2,4)));
回答by kelvincer
Also you can declare of this forms:
您也可以声明这种形式:
// first form
typedef vector<int> v1d;
typedef vector<v1d> v2d;
typedef vector<v2d> v3d;
v3d v(5, v2d(3, v1d(2, 4)));
// second form
vector<vector<vector<int> > > v = vector<vector<vector<int> > >( 5, vector<vector<int> >(3, vector<int>(2, 4)))