2D Vector in C++

Last Updated : 26 May, 2026

A 2D vector is a collection of multiple vectors stored inside another vector. It is mainly used to represent data in rows and columns, similar to a matrix or table. Since vectors are dynamic in nature, a 2D vector provides more flexibility than a traditional 2D array.

  • It allows dynamic resizing of rows and columns at runtime.
  • Different rows can store different numbers of elements.
  • It is widely used in matrix operations and graph representations.
C++
#include <iostream>
#include <vector>
using namespace std;

int main(){

    // Creating a 2D vector
    vector<vector<int>> v = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    };

    // Accessing elements
    cout << "Element at row 0, column 2: " << v[0][2] << endl;
    cout << "Element at row 1, column 3: " << v[1][3] << endl;

    return 0;
}

Output
Element at row 0, column 2: 3
Element at row 1, column 3: 8

Consider a normal 1D vector:

vector<int> v = {1, 2, 3, 4};

Here, all elements are stored in a single vector. Now, if we store multiple such vectors inside another vector, it becomes a 2D vector.

420046761
2D Vector

In the above diagram The outer vector contains three elements at indexes 0, 1, and 2, where each element stores another vector. For example:

  • Index 0 stores the vector {1, 2, 3, 4}
  • Index 1 stores the vector {5, 6, 7, 8}
  • Index 2 stores the vector {9, 10, 11, 12}

In this way, a 2D vector stores data in rows and columns similar to a matrix.


Syntax

vector<vector<data_type>> V;

Parameters:

  • data_type -> Type of elements stored in the vector.
  • V -> Name of the 2D vector

Creating a 2D Vector

In C++, a 2D vector can be created using the STL vector container by making a vector of vectors. Similar to normal vectors, 2D vectors can also be created in different ways.

Create an Empty 2D Vector

An empty 2D vector can be created and rows can be inserted dynamically using push_back().

C++
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<vector<int>> vec;

    vec.push_back({1, 2, 3});
    vec.push_back({4, 5, 6});

    for (const auto& row : vec)
    {
        for (int val : row)
            cout << val << " ";
        cout << endl;
    }

    return 0;
}

Output
1 2 3 
4 5 6 

Create a 2D Vector with Fixed Size and Default Value

A 2D vector can be initialized with a fixed number of rows and columns along with a default value.

C++
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<vector<int>> vec(3, vector<int>(4, 0));

    vec[0][0] = 5;
    vec[2][3] = 10;

    for (const auto& row : vec)
    {
        for (int val : row)
            cout << val << " ";
        cout << endl;
    }

    return 0;
}

Output
5 0 0 0 
0 0 0 0 
0 0 0 10 

Create a 2D Vector Using Initializer List

Values can also be directly assigned using curly braces.

C++
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<vector<int>> vec = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    for (const auto& row : vec)
    {
        for (int val : row)
            cout << val << " ";
        cout << endl;
    }

    return 0;
}

Output
1 2 3 
4 5 6 
7 8 9 

Basic Operations on 2D Vector

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.
C++
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<vector<int>> v = {{1, 2, 3}, {4, 5, 6}};

    v.push_back({7, 8, 9});
    v[1].insert(v[1].begin() + 1, 10);

    for (const auto& row : v)
    {
        for (int val : row)
            cout << val << " ";
        cout << endl;
    }

    return 0;
}

Output
1 2 3 
4 10 5 6 
7 8 9 

More insertion methods are discussed in - Insert Elements into 2D Vector

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.
C++
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<vector<int>> v = {{1, 2, 3}, {4, 5, 6}};

    cout << v[1][2] << endl;
    cout << v[0][1] << endl;

    v[0][1] = 9;

    cout << v[0][1] << endl;

    return 0;
}

Output
6
2
9

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().
C++
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<vector<int>> v = {{1, 2, 3}, {4, 5, 6}};

    v.erase(v.begin() + 1);
    v[0].erase(v[0].begin() + 1);

    for (const auto& row : v)
    {
        for (int val : row)
            cout << val << " ";
        cout << endl;
    }

    return 0;
}

Output
1 3 

Traversing 2D Vector

  • Iterate through each row and column using nested loops to access all elements by their indexes.
  • We can use the [] operator or the at() method to read or process each element during traversal.
C++
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<vector<int>> v = {{1, 2, 3}, {4, 5, 6}};

    for (const auto& row : v)
    {
        for (int val : row)
            cout << val << " ";
        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

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.
C++
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<vector<int>> vec = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    cout << "Rows: " << vec.size() << endl;
    cout << "Columns: " << vec[0].size() << endl;

    return 0;
}

Output
Rows: 3
Columns: 3

Common Operations and Applications

Apart from the basic operations, there are many operations that can be performed on 2D vectors:

Comment