• Create Account

### #ActualServant of the Lord

Posted 08 December 2012 - 01:04 AM

Why aren't you just using

int array[3][4][5][6];
//or
std::vector< std::vector< std::vector< std::vector< /*your Type*/ > > > > array;


Using a naked C++ array is usually bad for this and even if you have to if you use only one array you end up with a uniform grid like array only, jagged arrays wouldn't be possible you would need to use a "type****" for that and the just looks ugly.

Your first example and second example aren't equivalent.

While I agree with using an std::vector, I disagree with stacking them four deep.
A vector of a vector of a vector of a vector is not the same as a 4D array, in the same way that a vector of a vector is not the same as a 2D array, since each vector within the first vector aren't constrained to the same size (a so called 'jagged array' is an array of an array, not a 2D array).
If you have access to C++11, a std::array would constrain it to the same size.

When I want a multi-dimensional array, I use a single std::vector, and resize it to (width * height) or (width * height * depth), and index into it as Olof Hedman shows.

### #1Servant of the Lord

Posted 08 December 2012 - 01:03 AM

Why aren't you just using

int array[3][4][5][6];
//or
std::vector< std::vector< std::vector< std::vector< /*your Type*/ > > > > array;


Using a naked C++ array is usually bad for this and even if you have to if you use only one array you end up with a uniform grid like array only, jagged arrays wouldn't be possible you would need to use a "type****" for that and the just looks ugly.

While I agree with using an std::vector, I disagree with stacking them four deep.
A vector of a vector of a vector of a vector is not the same as a 4D array, in the same way that a vector of a vector is not the same as a 2D array, since each vector within the first vector aren't constrained to the same size (a so called 'jagged array' is an array of an array, not a 2D array IMO).
If you have access to C++11, a std::array would constrain it to the same size.

When I want a multi-dimensional array, I use a single std::vector, and resize it to (width * height) or (width * height * depth), and index into it as Olof Hedman shows.

PARTNERS