How to Reverse Iterate a Vector in C++?

Last Updated : 29 Jun, 2026

A vector stores elements in contiguous memory and is commonly traversed from beginning to end. However, in many situations, such as processing data in reverse order or displaying elements from last to first, it is necessary to iterate through a vector in reverse.

  • C++ provides reverse iterators (rbegin() and rend()) for efficient reverse traversal.
  • Reverse iteration can also be performed using indices, temporary containers, or by reversing the vector itself.

Methods to Reverse Iterate a Vector in C++

There are multiple ways to iterate through a vector in reverse order:

Using Reverse Iterators

The most efficient and recommended way to iterate through a vector in reverse order is by using reverse iterators returned by rbegin() and rend().

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

int main() {
    vector<int> v = {1, 3, 4, 7, 9};

    for (auto it = v.rbegin(); it != v.rend(); ++it)
        cout << *it << " ";

    return 0;
}

Output
9 7 4 3 1 

Explanation: rbegin() returns a reverse iterator pointing to the last element, while rend() points to the position before the first element. Traversing from rbegin() to rend() prints the vector in reverse order.

Using a Traditional Loop with Index

A vector can also be traversed in reverse order by starting from the last index and decrementing the index until the first element is reached.

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

int main() {
    vector<int> v = {1, 3, 4, 7, 9};

    for (int i = v.size() - 1; i >= 0; --i)
        cout << v[i] << " ";

    return 0;
}

Output
9 7 4 3 1 

Explanation: The loop starts from the last index (size() - 1) and moves backward until index 0.

Using a Temporary Reversed Vector

A temporary vector can be constructed using reverse iterators and then traversed using a range-based loop.

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

int main() {
    vector<int> v = {1, 3, 4, 7, 9};

    for (auto x : vector<int>(v.rbegin(), v.rend()))
        cout << x << " ";

    return 0;
}

Output
9 7 4 3 1 

Explanation: A temporary vector is created using the range [rbegin(), rend()], which stores the elements in reverse order.

Using reverse() Function

The vector can be reversed using the reverse() algorithm and then traversed normally.

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

int main() {
    vector<int> v = {1, 3, 4, 7, 9};

    reverse(v.begin(), v.end());

    for (auto x : v)
        cout << x << " ";

    return 0;
}

Output
9 7 4 3 1 

Explanation: The reverse() function rearranges the elements of the vector in reverse order, after which a normal traversal prints them from last to first.

Comparison of Methods

MethodModifies Original VectorExtra SpaceRecommended
Reverse IteratorsNoNoYes
Traditional Index LoopNoNoYes
Temporary Reversed VectorNoYesNo
reverse() FunctionYesNoOnly when modification is acceptable
Comment