How to Reverse iterate vector in C++

In this article, we are going to learn about how to iterate through the elements of a Vector in Reverse or Backward Direction. We will learn different ways to loop over vector elements from end to begin. The methods which we are going to learn are :

1. Using Indexing to Iterate over a vector in reverse order


The first method that we are going to learn is by using the indexes the same as we do for C++ arrays. In this example, we will iterate over a vector by using indexes. Vector indexes start at 0 and end at n-1, where n is the size of the vector.

Let us see this in the below code example:

#include<iostream>
#include<vector>

using namespace std;
int main()
{
    vector<int> intvec{ 1, 2, 3, 4, 5 };
    vector<string> strvec{ "Sachin", "Kohli", "Dhoni","Dravid","Sourav" };

    for (int i = intvec.size() - 1; i >= 0; i--)
    {
        cout << intvec[i] << ", ";
    }

    cout << endl;

    for (int i = strvec.size() - 1; i >= 0; i--)
    {
        cout << strvec[i] << ", ";
    }

    return 0;
}

Output

5, 4, 3, 2, 1, 
Sourav, Dravid, Dhoni, Kohli, Sachin,

2. By using the Reverse iterators to Iterate over a vector in backward direction


In this example, we are going to use the reverse iterators to iterate over a vector in the backward direction. We have two functions, rbegin() and rend() which help us to this iteration in the reverse direction.

Let us understand the use of these two functions with the help of our code example.

#include<iostream>
#include<vector>
using namespace std;
int main()
{

    vector<int> intvec{ 1, 2, 3, 4, 5 };
    vector<string> strvec{ "Sachin", "Kohli", "Dhoni","Dravid","Sourav" };

    // Using a for loop with reverse iterator
    for (vector<int>::reverse_iterator rit = intvec.rbegin(); rit != intvec.rend(); ++rit) 
    {
        std::cout << *rit << " ";
    }

    cout << endl;

    // Using a for loop with reverse iterator
    for (vector<string>::reverse_iterator rit = strvec.rbegin(); rit != strvec.rend(); ++rit) 
    {
        std::cout << *rit << " ";
    }

    return 0;
}

Output

5 4 3 2 1 
Sourav Dravid Dhoni Kohli Sachin

To shorten the syntax of the iterators we can make use of the auto keyword. The use of auto keywords helps developers to write code in short syntax. If you see the below-modified example, we are using auto in it.

#include<iostream>
#include<vector>
using namespace std;
int main()
{

    vector<int> intvec{ 1, 2, 3, 4, 5 };
    vector<string> strvec{ "Sachin", "Kohli", "Dhoni","Dravid","Sourav" };

    // Using a for loop with iterator
    for (auto rit = intvec.rbegin(); rit != intvec.rend(); ++rit) 
    {
        std::cout << *rit << " ";
    }

    cout << endl;

    // Using a for loop with iterator
    for (auto rit = strvec.rbegin(); rit != strvec.rend(); ++rit) 
   {
        std::cout << *rit << " ";
    }

    return 0;
}

Output

5 4 3 2 1 
Sourav Dravid Dhoni Kohli Sachin

In the below example, we are making use of the while loop instead of for loops. Here we are iterating using a while loop until we reach the end of the vector.

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<int> intvec{ 1, 2, 3, 4, 5 };
    vector<string> strvec{ "Sachin", "Kohli", "Dhoni","Dravid","Sourav" };

    std::vector<int>::reverse_iterator it = intvec.rbegin();
    while (it != intvec.rend())
    {
        cout << *it << ", ";
        it++;
    }

    cout << endl;

    auto its = strvec.rbegin();
    while (its != strvec.rend())
    {
        cout << *its << ", ";
        its++;
    }
    return 0;
}

Output

5, 4, 3, 2, 1, 
Sourav, Dravid, Dhoni, Kohli, Sachin,

3. By using Lambda function to Iterate over a vector in reverse.


In this example, we are going to make use of the for_each() loop and Lambda function to iterate over a vector in the reverse direction. In this loop, we will iterate over the vector using reverse iterators and for each iteration, we will call a lambda function that is going to print the value of the element of the vector.

Let us understand this with the help of the below code example.

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    vector<int> intvec{ 1, 2, 3, 4, 5 };
    vector<string> strvec{ "Sachin", "Kohli", "Dhoni","Dravid","Sourav" };

    for_each(intvec.rbegin(),
        intvec.rend(),
        [](const auto& elem) {
            std::cout << elem << ", ";
        });

    cout << endl;

    for_each(strvec.rbegin(),
        strvec.rend(),
        [](const auto& elem) {
            std::cout << elem << ", ";
        });

    return 0;
}

Output

5, 4, 3, 2, 1, 
Sourav, Dravid, Dhoni, Kohli, Sachin,

Summary

We hope you enjoyed learning different ways to iterate over a vector in reverse direction or backward direction.