Vector of multidimensional arrays

Is there a way to make vectors of multidimensional arrays? im trying to create a vector with 2-dimensional integer arrays as its elements, but vector<int[4][4]>does not work. What is the right way to do this?

+3
source share
3 answers

You cannot have array vectors. Standard container item types must be copyable, and arrays cannot be copied.

However, you can have a vector of vectors, for example:

std::vector<std::vector<int> >

Play with it.

Or to stick with arrays:

std::vector<boost::array<int, N> >

Or if you have C ++ 0x:

std::vector<std::array<int, N> >

{boost,std}::array - This is a wrapper of objects around arrays of automatic storage duration, so it is pretty close to what you originally tried to execute.

+3

As @vrince explained in a comment, you can create a class / structure encapsulation int[4][4]:

struct My2DArray {
  int a[4][4];
};

And declare vectorfor above:

vector<My2DArray> obj;

For convenience, you can define various methods operatorand public(for example, copy constructor, assignment operator, etc.) to deal with it.

0
source

All Articles