How to Reverse Iterate a Vector in C++? Last Updated : 19 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will learn different methods to iterate through the vector in reverse order in C++.The most efficient method to iterate through the vector in reverse order is by using reverse iterator. Let’s take a look at an example: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 3, 4, 7, 9}; // Iterating the vector in reverse order for (auto i = v.rbegin(); i != v.rend(); i++) cout << *i << " "; return 0; } Output9 7 4 3 1 Explanation: We used reverse iteratorvector rbegin()and vector rend(). By iterating from rbegin() to rend(), we can print the elements of vector in reverse order.There are also some other methods in C++ to iterate through the vector in reverse order. Some of them are as follows:Table of ContentUsing Traditional Loop with IndexUsing Range Based Loop with Reverse IteratorUsing for_each()Using reverse() Function with LoopUsing IndexA vector can be traverse in reverse order using for loop by starting from the last index and decrementing the index until we reach the first element. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 3, 4, 7, 9}; // Iterating vector in reverse order for (int i = v.size() - 1; i >= 0; i--) cout << v[i] << " "; return 0; } Output9 7 4 3 1 Using a Temporary Reversed VectorCreate a temporary reversed vector using reverse iterator and then traverse it using range-based loop. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 3, 4, 7, 9}; // Iterating the vector in reverse order for (auto &i : vector<int>(v.rbegin(), v.rend())) cout << i << " "; return 0; } Output9 7 4 3 1 Using reverse()Reverse the vector using reverse() function, traverse the vector normally and reverse the vector again after traversal. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 3, 4, 7, 9}; // reverse the vector reverse(v.begin(), v.end()); // Iterating through vector for (auto i : v) cout << i << " "; return 0; } Output9 7 4 3 1 Comment More infoAdvertise with us Next Article How to Reverse a List in C++ STL? G gaurav472 Follow Improve Article Tags : C++ Programs C++ cpp-iterator STL cpp-vector CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Iterate Over a Vector of Pairs in C++? In C++, vectors are dynamic arrays that allow users to store data in a contiguous memory location while pairs are data structures that allow the users to store two heterogeneous values together in the key-value pair. In this article, we are to learn how we can iterate over a vector of pairs in C++. 2 min read How to Iterate Through a Vector in C++? Iterating or traversing a vector means accessing each element of the vector sequentially. In this article, we will learn different methods to iterate over a vector in C++.The simplest way to iterate a vector is by using a range based for loop. Let's take a look at a simple example that iterate the v 3 min read How to Iterate 2D Vector in C++? 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 3 min read How to Reverse a List in C++ STL? In C++, std::list is a sequence container that allows non-contiguous memory allocation. As such, it is a doubly linked list that can be traversed in both directions. In this article, we will learn how to reverse a list in C++. Example: Input: myList = {10, 20, 30, 40, 50}; Output: Reversed List: 50 2 min read How to Use a reverse_Iterator with a Map in C++? In C++, a reverse_iterator is a type of iterator that points to elements in a container in the reverse order. This means that using a reverse_iterator, you can traverse a container from end to beginning. In this article, we will learn how to use a reverse_iterator with a map in C++. Example: Input: 2 min read How to Reverse a Vector using STL in C++? Reversing the vector means the swapping last element and first element, second last element and second element, and so on. In this article, we will learn how to reverse a vector using STL in C++.The most efficient method to reverse the vector is by using reverse() function. Letâs take a look at a si 3 min read Like