![]() |
VOOZH | about |
In C++, resizing the 2D vector means increasing or decreasing the row and column size of 2D vector. In this article, we will learn different methods to resize a 2D vector in C++.
The most efficient way to resize the 2D vector is by using vector resize() function. Let’s take a look at a simple example:
Rows: 2 Cols: 3
Explanation: The column size (number of rows) can be changed using vector resize() on the 2D vector, but the row size (number of columns) has to be changed one by one for individual member vector.
C++ also provides other methods to resize the 2D vector. Some of them are as follows:
Table of Content
If the 2D vector is empty, then the vector resize() function can directly resize the rows and columns simultaneously in a single line.
Rows: 2 Cols: 3
This method cannot be used on the vectors that contains elements.
The vector assign() method works by assigning a newly created 2d vector of desired size to our current vector.
Rows: 2 Cols: 3
Vector assign() creates a new vector, removing all previously stored elements.