2D Vector in C++
A 2D vector is a vector of the vectors i.e. each element is a vector itself.
- It can be visualized as a matrix where each inner vector represents a row, and the number of rows represents the maximum columns.
- The advantage over 2D arrays are, we can have dynamically sized rows and columns, i.e., every row can have different number of columns (useful in problems like adjacency list representation of graphs), flexible in size and no extra parameters required while passing to function
- The disadvantage is, individual rows might be stored at different locations (all items of a row are stored at same location though) as vectors internally use dynamic memory. So it might not be as cache friendly as a normal 2D array.

#include <iostream>
#include <vector>
using namespace std;
int main()
{
// Create a jagged 2D vector named vec
vector<vector<int>> vec = {{1, 2}, {5, 6, 7, 8}, {9}, {9, 8, 11}};
// Display the jagged array
for (int i = 0; i < vec.size(); i++)
{
cout << i << " -> ";
for (int j = 0; j < vec[i].size(); j++)
{
cout << vec[i][j] << " ";
}
cout << endl;
}
return 0;
}
Output
0 -> 1 2 1 -> 5 6 7 8 2 -> 9 3 -> 9 8 11
Syntax
vector<vector<data_type>> V;
where, data_type is the type of elements and V is the name assigned to the 2D vector.
Creating a 2D Vector
In C++, a 2D vector can be created using the STL vector by making a vector of vectors.
Just like vectors, a 2D vector can be created and initialized in multiple ways:
1. Default (Empty)
Create an empty 2D vector and add rows dynamically using push_back().
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// empty 2D vector
vector<vector<int>> vec;
// Adding rows dynamically
vec.push_back({1, 2, 3});
vec.push_back({4, 5, 6});
for (int i = 0; i < vec.size(); i++)
{
for (int j = 0; j < vec[i].size(); j++)
{
cout << vec[i][j] << " ";
}
cout << endl;
}
return 0;
}
Output
1 2 3 4 5 6
2. With User Defined Size and Default Value
Create a 2D vector with fixed number of rows and columns, and initialize all elements to a default value.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// 3 rows, 4 columns, all elements initialized to 0
vector<vector<int>> vec(3, vector<int>(4, 0));
// Modifying elements
vec[0][0] = 5;
vec[2][3] = 10;
for (int i = 0; i < vec.size(); i++)
{
for (int j = 0; j < vec[i].size(); j++)
{
cout << vec[i][j] << " ";
}
cout << endl;
}
return 0;
}
Output
5 0 0 0 0 0 0 0 0 0 0 10
3. Using Initializer List
Create a 2D vector and directly assign values using curly braces.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// Directly initializing with values
vector<vector<int>> vec = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int i = 0; i < vec.size(); i++)
{
for (int j = 0; j < vec[i].size(); j++)
{
cout << vec[i][j] << " ";
}
cout << endl;
}
return 0;
}
Output
1 2 3 4 5 6 7 8 9
Basic Operations on 2D Vector
1. Inserting Elements
- Elements can be added to a 2D vector by inserting a new row using push_back().
- Individual elements can also be added to an existing row by using push_back() on that specific row.
#include <iostream.h>
#include <vector>
using namespace std;
int main()
{
vector<vector<int>> v = {{1, 2, 3}, {4, 5, 6}};
// Insert a new row at the end
v.push_back({7, 8, 9});
// Insert value in 2nd row at 2nd position
v[1].insert(v[1].begin() + 1, 10);
for (int i = 0; i < v.size(); i++)
{
for (int j = 0; j < v[i].size(); j++)
{
cout << v[i][j] << " ";
}
cout << endl;
}
return 0;
}
Output
1 2 3 4 10 5 6 7 8 9
More methods of insertion are discussed here - Insert Elements into 2D Vector
2. Accessing and Updating Elements
- As 2D vectors are organized like matrices with rows and columns, we need two indexes to access an element: one for the row number (i) and one for the column number (j).
- We can access elements using the [] operator or the at() method.
- The value of an accessed element can be updated by assigning a new value using the = operator.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<vector<int>> v = {{1, 2, 3}, {4, 5, 6}};
// Access 3rd element in 2nd row
cout << "3rd element in 2nd row: " << v[1][2] << endl;
// Access 2nd element in 1st row
cout << "2nd element in 1st row: " << v[0][1] << endl;
// Updating the 2nd element in 1st row
v[0][1] = 9;
cout << "2nd element in 1st row after updating: " << v[0][1] << endl;
return 0;
}
Output
3rd element in 2nd row: 6 2nd element in 1st row: 2 2nd element in 1st row after updating: 9
3. Deleting Elements
- Similar to insertion, there are two types of deletion in a 2D vector: deleting a row and deleting a value in an existing row.
- To delete an entire row, use the erase() method on the main vector with the row index. To delete a specific element in a row, use erase() on the row vector with the column index.
- To remove the last element of a row or the last row of the 2D vector, use
pop_back().
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<vector<int>> v = {{1, 2, 3}, {4, 5, 6}};
// Delete the second row
v.erase(v.begin() + 1);
// Delete second element in first row
v[0].erase(v[0].begin() + 1);
for (int i = 0; i < v.size(); i++)
{
for (int j = 0; j < v[i].size(); j++)
{
cout << v[i][j] << " ";
}
cout << endl;
}
return 0;
}
Output
1 3
4. Traversing 2D Vector
- Iterate through each row and column using nested loops to access all elements by their indexes.
- You can use the [] operator or the at() method to read or process each element during traversal.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<vector<int>> v = {{1, 2, 3}, {4, 5, 6}};
// Loop through rows
for (int i = 0; i < v.size(); i++)
{
// Loop through columns
for (int j = 0; j < v[i].size(); j++)
cout << v[i][j] << " ";
cout << endl;
}
return 0;
}
Output
1 2 3 4 5 6
C++ provides more methods to traverse 2D vector which are discussed in this article - Iterate 2D Vector
5. Finding Size of 2D Vector
- Finding the size of a 2D Vector involves finding its row size and column size.
- Row Size: Use the size() method on the main (outer) vector to get the number of rows.
- Column Size: Use the size() method on an inner vector to get the number of columns in that specific row, as rows can have different sizes.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// Creating a 2D vector
vector<vector<int>> vec = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
// Finding the number of rows (size of the outer vector)
int rows = vec.size();
cout << "Number of rows: " << rows << endl;
// Finding the number of columns (size of any inner vector first row)
int cols = vec[0].size();
cout << "Number of columns: " << cols << endl;
return 0;
}
Output
Number of rows: 3 Number of columns: 3
Common Operations and Applications
Apart from the basic operations, there are many operations that can be performed on 2D vectors: