Open In App

Vector of Vectors in C++ STL with Examples

Last Updated : 26 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, a vector of Vectors is a two-dimensional vector with a variable number of rows, where each row is a vector. Each index of a vector stores a vector that can be traversed and accessed using iterators. It is similar to an Array of Vectors but with dynamic properties.

Syntax:

C++
vector<vector<data_type>> vec;

where vec is the name of a 2D vector, and data_type is the type of elements.

Initialization

In the most of the cases, the first operation of a vector of vectors is initialized with a few elements, either by providing default values with a specified size or using an initialization list.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    
    // 2D vector with initial 
    // size and value
    vector<vector<int>> v1(3, vector<int>(3, 7));
  
    // A 2D vector initialized 
    // with initializer list
    vector<vector<int>> v2 = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    return 0;
}

To more about initialization of vector of vectors, refer to the article – Initialize 2D Vector

Accessing and Updating Elements

Arrays are accessed using their indexes, while a vector of vectors represents as matrix, requires both row and column values as indexes to access or update any element. We can use either the [] operator or the at() function to access and update elements in a vector of vectors.

C++
#include <bits/stdc++.h>
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, 4);
    
    // Access first element 
    // of the first row
    cout << v[0][0] << endl;
    
    // Update first element 
    // of the second row
    v[1][0] = 5;
    
    // Access first element
    // of the second row using at()
    cout << v.at(1).at(0);
    
    return 0;
}

Output
1
5

Traversing Vector of Vectors

To traverse a 2D vector, you need to loop through each row and column using nested loops, and then access the elements by their respective indices.

C++
#include <bits/stdc++.h>
using namespace std;

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

    // Outer loop for rows
    for (int i = 0; i < v.size(); i++) {
        
        // Inner loop for 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 
7 8 6 

Traverse a vector in multiple ways - Iterator 2D vector

Insertion in Vector of Vectors

In a vector of vectors, values can be added as a new row or inserted into an existing row. You can insert values at any position using the insert() function, or add them at the end using push_back().

C++
#include <bits/stdc++.h>
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, 4);
    
    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 4 5 6 
7 8 9 

Different ways to insert elements - Insert Elements into vector of vectors

Removal or Deletion in a Vector of Vectors

In a 2D vector, deletion can be performed in two ways:

  1. Removing a row
  2. Deleting a specific value within a row

To delete elements, you can use the erase() function for removing an element at a particular position or range. Alternatively, the pop_back() function can be used to remove the last element or row of the vector. Here’s an example illustrating how these operations work:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<vector<int>> v = {{1, 2, 3},
                            {4, 5, 6},
                            {7, 8, 6}};
    
    // Delete the first row
    v.erase(v.begin());
    
    // Delete second element 
    // in first row
    v[0].erase(v[0].begin() + 1);
    
    // Delete the last row
    v.pop_back();
    
    // Now array have only one row
    cout << v[0][0] << " " << v[0][1];
    return 0;
}

Output
4 6

Next Article
Article Tags :
Practice Tags :

Similar Reads