Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resizing multidimensional vector

Tags:

c++

stl

How to resize multidimensional vector such as:

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

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

like image 253
qutron Avatar asked Nov 30 '10 08:11

qutron


People also ask

How do you resize a 2D vector?

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.

Can a vector be resized?

Vectors are known as dynamic arrays which can change its size automatically when an element is inserted or deleted.

How do you resize vector vectors?

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.

How the size of a STL vector increases once it is full?

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.


3 Answers

array.resize(3,vector<vector<custom_type> >(5,vector<custom_type>(10)));
like image 73
Matt Avatar answered Oct 15 '22 18:10

Matt


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);
    }
}
like image 40
qutron Avatar answered Oct 15 '22 20:10

qutron


see also Boost.MultiArray

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

like image 6
davka Avatar answered Oct 15 '22 20:10

davka