How to resize multidimensional vector such as:
vector <vector <vector <custom_type> > > array;
F开发者_开发问答or example, I need array[3][5][10]?
array.resize(3,vector<vector<custom_type> >(5,vector<custom_type>(10)));
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);
}
}
see also Boost.MultiArray
Boost.MultiArray provides a generic N-dimensional array concept definition and common implementations of that interface.
You should resize all the nested vector
s one by one. Use nested for
loops or recursion.
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[],...)
精彩评论