How to resize multidimensional vector such as:
vector <vector <vector <custom_type> > > array;
For example, I need array[3][5][10]?
Resize 2D Vector in C++ To resize the 2d vector in C++, we need to use a function called resize() , which is part of the STL library. The resize() method takes two parameters, both of which are integers. The first integer specifies the new length of the vector, and the second integer specifies its new width.
Vectors are known as dynamic arrays which can change its size automatically when an element is inserted or deleted.
The C++ function std::vector::resize() changes the size of vector. If n is smaller than current size then extra elements are destroyed. If n is greater than current container size then new elements are inserted at the end of vector. If val is specified then new elements are initialed with val.
it happens when we push_back() and the capacity is already full (i.e. v. size() == v. capacity() ), it has to be noted that it doesn't happen a little bit before. the capacity increases to 1.5 times the previous capacity.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With