How to Iterate 2D Vector in C++? Last Updated : 23 Jul, 2025 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; } Output1 2 3 4 5 6 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:Table of ContentUsing IndexesUsing IteratorsUsing Reverse IteratorsUsing IndexesThis 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; } Output1 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 IteratorsThe 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; } Output1 2 3 4 5 6 Using Reverse IteratorsTo 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; } Output6 5 4 3 2 1 Comment More infoAdvertise with us A abhishekcpp Follow Improve Article Tags : C++ Programs C++ STL cpp-vector CPP Examples +1 More Practice Tags : CPPSTL Explore C++ Programming Language 5 min read C++ OverviewIntroduction to C++ Programming Language 3 min read Features of C++ 5 min read History of C++ 7 min read Interesting Facts about C++ 2 min read Setting up C++ Development Environment 8 min read Difference between C and C++ 3 min read C++ BasicsUnderstanding First C++ Program 4 min read C++ Basic Syntax 4 min read C++ Comments 3 min read Tokens in C 4 min read C++ Keywords 2 min read Difference between Keyword and Identifier in C 3 min read C++ Variables and ConstantsC++ Variables 4 min read Constants in C 4 min read Scope of Variables in C++ 7 min read Storage Classes in C++ with Examples 6 min read Static Keyword in C++ 5 min read C++ Data Types and LiteralsC++ Data Types 7 min read Literals in C 4 min read Derived Data Types in C++ 4 min read User Defined Data Types in C++ 4 min read Data Type Ranges and Their Macros in C++ 3 min read C++ Type Modifiers 4 min read Type Conversion in C++ 4 min read Casting Operators in C++ 5 min read C++ OperatorsOperators in C++ 9 min read C++ Arithmetic Operators 4 min read Unary Operators in C 5 min read Bitwise Operators in C 6 min read Assignment Operators in C 4 min read C++ sizeof Operator 3 min read Scope Resolution Operator in C++ 4 min read C++ Input/OutputBasic Input / Output in C++ 5 min read cin in C++ 4 min read cout in C++ 2 min read Standard Error Stream Object - cerr in C++ 2 min read Manipulators in C++ 4 min read C++ Control StatementsDecision Making in C (if , if..else, Nested if, if-else-if ) 7 min read C++ if Statement 3 min read C++ if else Statement 3 min read C++ if else if Ladder 3 min read Switch Statement in C++ 5 min read Jump statements in C++ 4 min read C++ Loops 7 min read for Loop in C++ 6 min read Range-Based for Loop in C++ 3 min read C++ While Loop 3 min read C++ do while Loop 4 min read C++ FunctionsFunctions in C++ 8 min read return Statement in C++ 4 min read Parameter Passing Techniques in C 3 min read Difference Between Call by Value and Call by Reference in C 4 min read Default Arguments in C++ 5 min read Inline Functions in C++ 6 min read Lambda Expression in C++ 4 min read C++ Pointers and ReferencesPointers and References in C++ 5 min read C++ Pointers 8 min read Dangling, Void , Null and Wild Pointers in C 6 min read Applications of Pointers in C 4 min read Understanding nullptr in C++ 3 min read References in C++ 5 min read Can References Refer to Invalid Location in C++? 2 min read Pointers vs References in C++ 5 min read Passing By Pointer vs Passing By Reference in C++ 5 min read When do we pass arguments by pointer? 5 min read Like