qutron
qutron

Reputation: 1760

resizing multidimensional vector

How to resize multidimensional vector such as:

  vector <vector <vector <custom_type> > > array; 

For example, I need array[3][5][10]?

Upvotes: 11

Views: 37190

Answers (5)

Tony Delroy
Tony Delroy

Reputation: 106096

You should resize all the nested vectors one by one. Use nested for loops or recursion.

Upvotes: 4

Matt
Matt

Reputation: 20786

array.resize(3,vector<vector<custom_type> >(5,vector<custom_type>(10)));

Upvotes: 18

davka
davka

Reputation: 14682

see also Boost.MultiArray

Boost.MultiArray provides a generic N-dimensional array concept definition and common implementations of that interface.

Upvotes: 6

stefaanv
stefaanv

Reputation: 14392

I would make a custom container containing a vector of vectors (of ... per dimension) and resize with resize-functions per dimension. This way you can put the invariant of equal size per dimension in one place. The actual resizing can then be done in a loop according to dimension.
There will be a bit of work involved in making public what needs to be accessed (operator[],...)

Upvotes: 2

qutron
qutron

Reputation: 1760

I did it))

array.resize(3);
for (int i = 0; i < 3; i++)
{
    array[i].resize(5);
    for (int j = 0; j < 5; j++)
    {
       array[i][j].resize(10);
    }
}

Upvotes: 7

Related Questions