Methods to resize a 2D vector in C++

In this tutorial, I’ll explain how to resize a 2D vector in C++ with some easy examples and code snippets. Before starting this let’s understand how to initialize a 2D vector in C++.

Initialize a 2D vector in C++

#include"bits/stdc++.h"
using namespace std;

int main(){
  vector<vector<int>>V;
  cout<<"The number of rows in vector V is "<<V.size()<<endl;
}

Output

The number of rows in vector V is 0

This is a simple code snippet on how you can initialize a 2D vector. We didn’t specify any number of rows or columns for this vector. It doesn’t have any specified size yet; by default, it is 0x0. Vectors have dynamic memory allocations and we can change the shape and size of a vector at any instant. (Don’t forget to always include the header file “bits/stdc++.h”)

Resizing a 2D vector using STL keyword .resize() and vector default constructor

#include"bits/stdc++.h"
using namespace std;

int main(){
  vector<vector<int>>V;
  V.resize(4, vector<int>(3));
  cout<<"The number of rows in vector V is "<<V.size()<<endl;
  cout<<"The number of columns in vector V is "<<V[0].size()<<endl;
}

Output

The number of rows in vector V is 4
The number of columns in vector V is 3

Time Complexity

If the number of rows is N and a number of columns is M, then the time complexity of this 
code is O(N*M).

Space Complexity

If the number of rows is N and the number of columns is M, then the space complexity of this 
code is O(N*M).

We used the .resize() command, which is an STL keyword that can be used for a vector to describe its shape and size. Here in this example code snippet, the dimension of the vector is now specified as 4×4 using the STL keyword .resize().

2D Vector with variable cells in each row

Next, we learn about how to change the number of cells in each row of the 2D vector. Suppose in that previous example of vector V, we wanted to have 1 cell in rows 2 and 3 instead of 3. How will this task be performed?

#include"bits/stdc++.h"
using namespace std;

int main(){
  vector<vector<int>>V;
  V.resize(4, vector<int>(3));
  for(int i=0; i<4; i++)
  {
    if(i==2 || i==3)
    V[i].resize(1);
  }
  cout<<"The number of cells in row 0 of vector V is "<<V[0].size()<<endl;
  cout<<"The number of cells in row 1 of vector V is "<<V[1].size()<<endl;
  cout<<"The number of cells in row 2 of vector V is "<<V[2].size()<<endl;
  cout<<"The number of cells in row 3 of vector V is "<<V[3].size()<<endl;
}

Output

The number of cells in row 0 of vector V is 3
The number of cells in row 1 of vector V is 3
The number of cells in row 2 of vector V is 1
The number of cells in row 3 of vector V is 1

Above mentioned code snippet changes the number of cells in each desired row of the 2D vector V. The unchanged rows, remains unaffected.

Changing the number of rows of 2D vector

If we just want to change the number of rows of the vector V, we follow the following code mentioned in the code snippet below:

#include"bits/stdc++.h"
using namespace std;

int main(){
  vector<vector<int>>V;
  V.resize(4, vector<int>(3));
  V.resize(6);
  cout<<"The number of rows in vector V is "<<V.size()<<endl;
  cout<<"The number of columns in vector V is "<<V[0].size()<<endl;
}

Output

The number of rows in vector V is 6
The number of columns in vector V is 3

You can observe through the output, that the number of columns remains unaffected, and we were successful in changing the number of rows of the 2D vector V.

Resizing a 2D vector along with changing the default value at every cell in a row

#include"bits/stdc++.h"
using namespace std;

int main(){
  vector<vector<int>>V;
  V.resize(4, vector<int>(3));
  for(int i=0; i<4; i++)
  {
    for(auto it: V[i])
    cout<<it<<" ";
    cout<<endl;
  }
}

Output

0 0 0
0 0 0
0 0 0
0 0 0

You can observe through the output, that every cell in each row is filled with default value 0. Now, suppose we want to resize the vector V to dimension 5×6 with every cell filled with value 1.

#include"bits/stdc++.h"
using namespace std;

int main(){
  vector<vector<int>>V;
  V.resize(4, vector<int>(3));
  V.clear();
  V.resize(5, vector<int>(5,1));
  for(int i=0; i<5; i++)
  {
    for(auto it: V[i])
    cout<<it<<" ";
    cout<<endl;
  }
}

Output

1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

You can observe in the code snippet, that we first cleared the initial 2D vector and then initialized a 4×5 vector with with each cell filled with 1.

Resizing a 2D vector using STL keywords .resize() and .push_back()

#include"bits/stdc++.h"
using namespace std;

int main(){
  vector<vector<int>>V;
  V.resize(4);
  for(int i=0; i<4; i++)
  {
    for(int j=0; j<3; j++)
    V[i].push_back(0);
  }
  cout<<"The number of rows in vector V is "<<V.size()<<endl;
  cout<<"The number of columns in vector V is "<<V[0].size()<<endl;
}

Output

The number of rows in vector V is 4
The number of columns in vector V is 3

We can observe, that once again we can create a 2D vector using another STL command .push_back() along with .resize(). In the given code snippet we created a 4×3 Vector V, with each cell filled with 0. We can also change the value of each cell by changing the value ‘x’ in the push_back(x) command.

Leave a Reply

Your email address will not be published. Required fields are marked *