Open In App

How to Iterate 2D Vector in C++?

Last Updated : 22 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Iterating or traversing a 2D vector means accessing each element of the 2D vector sequentially. In this article, we will explore different methods to iterate over a 2D vector in C++.

The simplest way to iterate a 2D vector is by using a range-based for loop. Let's take a look at a simple example that iterates over a 2D vector using a range-based for loop:

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

int main() {
    vector<vector<int>> v = {{1, 2, 3}, 
                             {4, 5, 6}};
  
    // Iterating using range-based for loop
    for (const auto& row : v) {
        for (const auto& e : row) {
            cout << e << " ";
        }
        cout << endl;
    }

    return 0;
}

Output
1 2 3 
4 5 6 
7 8 9 

The range-based for loop allows users to iterate through the vector rows and their elements without worrying about indexes or iterators.

The above method is only preferred when iterating the entire 2D vector. C++ also provides other methods to iterate through 2D vectors that may be more relevant in some cases:

Using Indexes

This method is the traditional way to traverse a 2D vector, where each element is accessed using its row and column index. The size of the vector and rows can be determined using the vector size() method.

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

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

    // Iterating using indexes
    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 5 6 

This method provides full control over the iteration and is particularly useful when the row and column indexes are needed for some processing.

Using Iterators

The vector begin() and end() methods of a vector return iterators to its first and last elements, respectively. These iterators can be used to iterator 2D vectors by using nested loops.

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

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

    // Iterating using iterators
    for (auto i = v.begin(); i != v.end(); i++) {
        for (auto j = i->begin(); j != i->end(); j++) {
            cout << *j << " ";
        }
        cout << endl;
    }

    return 0;
}

Output
1 2 3 
4 5 6 

Using Reverse Iterators

To iterate through a 2D vector in reverse order, the vector rbegin() and rend() methods can be used. These reverse iterators traverse the elements from the last to the first.

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

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

    // Iterating using reverse iterators
    for (auto i = v.rbegin(); i != v.rend(); ++i) {
        for (auto j = i->rbegin(); j != i->rend(); ++j) {
            cout << *j << " ";
        }
        cout << endl;
    }

    return 0;
}

Output
6 5 4 
3 2 1 



Next Article
Article Tags :
Practice Tags :

Similar Reads