Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to create a 3 dimensional vector in c++

Tags:

c++

So, im trying to create a 3 dimensional 5x3x2 vector, using the vector lib and saving the number 4 in every node.

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:

vector<vector<int> > vec (5,vector <int>(8,6));
like image 830
Mete Avatar asked Mar 21 '12 20:03

Mete


2 Answers

You almost got it right -- the second nested vector should be vector<vector<int> >, not just a vector<int>:

vector<vector<vector<int> > > vec (5,vector<vector<int> >(3,vector <int>(2,4)));
like image 184
Sergey Kalinichenko Avatar answered Sep 30 '22 14:09

Sergey Kalinichenko


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)))
like image 23
kelvincer Avatar answered Sep 30 '22 13:09

kelvincer