Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

3D vector

Status
Not open for further replies.

Maurader

Technical User
May 8, 2002
59
0
0
CA
is there a way to create a vector of vectors of vectors? and how would i initialize such a beast?

Thanks!
 
You could do it this way (Sorry if it wraps) :



#include <vector>
using namespace std;

void main(void)
{

typedef vector<int> vec_1d_T;
typedef vector<vec_1d_T> vec_2d_T;
typedef vector<vec_2d_T> vec_3d_T; // <<==== This is your type


vec_1d_T vec_1d;
vec_2d_T vec_2d;
vec_3d_T vec_3d;

// Populate 1d vector :
vec_1d.push_back(81);
vec_1d.push_back(82);
vec_1d.push_back(83);
vec_1d.push_back(84);


// Populate 2d vector :
vec_2d.push_back(vec_1d);
vec_2d.push_back(vec_1d); // Lazy programmer, reuse 1d vector

// Populate 3d vector :
vec_3d.push_back(vec_2d);
vec_3d.push_back(vec_2d); // Lazy ...
vec_3d.push_back(vec_2d); // ...


// Run through 3d vector :
printf(&quot;\nSize of 3d vector is %d&quot;, vec_3d.size()); // Should be 3
for(int ix1 = 0; ix1 < vec_3d.size(); ix1++) {
printf(&quot;\n\n%d. 2d vector : size = %d&quot;, ix1+1, vec_3d[ix1].size());
for(int ix2 = 0; ix2 < vec_3d[ix1].size(); ix2++) {
printf(&quot;\n\n%d. 1d vector : size = %d&quot;, ix2+1, vec_3d[ix1][ix2].size());
for(int ix3 = 0; ix3 < vec_3d[ix1][ix2].size(); ix3++) {
printf(&quot;\n%d. element contains %d&quot;, ix3+1, vec_3d[ix1][ix2][ix3]);
}
}
}
}




You could also declare it like this :
vector<vector<vector<int> > > ThreeDIM;

!! Remember space between closing angles !!


/JOlesen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top