In this article, we will discuss three different ways to iterate or loop over a C++ Vector in reverse direction.
Subscribe to our Youtube channel for more videos on C++ / C++ / STL
C++: Iterate over a vector in reverse order using Indexing
Suppose we have a vector of size N, then we can iterate over the vector in reverse order by traversing from index position N-1 to 0. For example,
#include<iostream> #include<vector> using namespace std; int main() { // Create a vector of integers vector<int> vec_of_num{1, 3, 4, 7, 8, 9}; // Iterate over a vector in backward direction using // indexing for(int i = vec_of_num.size() - 1; i >= 0; i--) { std::cout<<vec_of_num[i]<<", "; } return 0; }
Output:
9, 8, 7, 4, 3, 1,
The benefit of this approach is that you will also be aware of indexes of elements while iteration.
C++: Iterate over a vector in reverse order using Reverse Iterator
A reverse iterator is a kind of iterator, that travels in backwards direction.
It means when we increment a reverse_iterator, it goes to the previous element in container. So, to iterate over a vector in reverse direction, we can use the reverse_iterator to iterate from end to start.
Frequently Asked:
vector provides two functions which returns a reverse_iterator i.e.
- vector::rbegin() –> Returns a reverse iterator that points to the last element of vector
- vector::rend() –> Returns a reverse iterator that points to the virtual element before the start of vector.
Now we can use a while loop to traverse from end to start in a vector using reverse_iterator returned by the functions rbegin() and rend(). For example,
#include<iostream> #include<vector> using namespace std; int main() { // Create a vector of integers vector<int> vec_of_num{1, 3, 4, 7, 8, 9}; // Iterate over a vector in backward direction using // reverse iterators std::vector<int>::reverse_iterator it = vec_of_num.rbegin(); while (it != vec_of_num.rend()) { std::cout<<*it<<", "; it++; } return 0; }
Output:
9, 8, 7, 4, 3, 1,
Read More
Best Resources to Learn C++:
C++: Iterate over a vector in reverse order in single line
In the previous example, we used a while loop to iterate over a vector using reverse_iterator. But we can do same in a single line using STL Algorithm for_each().
Using STL Algorithm for_each(start, end, callback), we can iterate over all elements of a vector in a single line. It accepts three arguments i.e.
- Start Iterator -> Iterator pointing to the start of a range
- End Iterator -> Iterator pointing to the End of a range
- Callback Function -> A function that needs to be applied to all elements in the range from start to end.
for_each() iterates over all the elements between start & end iterator and apply the given callback on each element.
If we pass the reverse iterators returned by rbegin() and rend() to for_each(), then it will iterate over the elements of vector in reverse direction and apply a lambda function on each item of the vector. Inside the lambda function we can perform any operation like printing the elements or any other operation. For example,
#include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { // Create a vector of integers vector<int> vec_of_num{1, 3, 4, 7, 8, 9}; // Iterate over a vector in backward direction using // reverse iterators, for_each() and Lambda function std::for_each( vec_of_num.rbegin(), vec_of_num.rend(), [](const auto & elem){ std::cout<<elem<<", "; }); return 0; }
Output:
9, 8, 7, 4, 3, 1,
We iterated over all elements of a vector in reverse direction.
Summary
We learned about three different ways to iterate or loop over a vector in reverse direction.