Open In App

vector rbegin() and rend() Functions in C++ STL

Last Updated : 24 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, std::vector::rbegin() and std::vector::rend() are built-in functions used to retrieve reverse iterators to the reverse beginning and reverse end of a vector. These functions allow easy traversal of vectors in reverse i.e. from the last element to the first. They are the member functions of the std::vector class, defined inside <vector> header file.

Example:

C++
// C++ Program to illustrate the use of
// vector::rbegin() and vector::rend()
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {9, 11, 15, 56};

    cout << *v.rbegin() << endl;
    cout << *--v.rend();
  
    return 0;
}

Output
56
9

You may have noticed we have decremented the vector::rend() iterator before dereferencing. The reason is that rend() points to the theoretical element before the first element in the reverse order.

vector::begin() Method

The std::vector::rbegin() method is used to retrieve a vector::reverse_iterator pointing to the last element (reverse beginning) of the std::vector container.

Syntax

v.rbegin()

Parameters

  • This function does not take any parameters.

Return Value

  • Returns a reverse iterator pointing to the last element of the vector container.
  • If the vector is empty, it returns the same iterator as vector::rend().

vector::rend() Method

The std::vector::rend() method is used to retrieve a vector::reverse_iterator pointing to the theoretical element that comes before the first element (reverse end) of the vector. We have to be carful while using it as it does not point to the first element. We have to decrement it to access the first element.

Syntax

v.rend()

Parameters

  • This function does not take any parameters.

Return Value

  • Returns a reverse iterator pointing to the position before the first element of the vector.

Supported Operations

In C++, vector uses random access iterators, so the iterators returned by vector::rbegin() and vector::rend() functions support all the operations allowed in the iterator arithmetic in C++ i.e. dereferencing, incrementing/decrementing, adding/subtracting integers, subtraction of another iterator of same container, comparision.

More Examples of vector::rbegin() and vector::rend()

Example 1: Traversing Vector in Reverse

We can traverse the vector in reverse order by incrementing and dereferencing the vector::rbegin() iterator until it is not equal to the vector::rend() iterator.

C++
// C++ program to traverse vector in reverse
// using vector::rbegin() and vector::rend()
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {9, 11, 15, 56};

    // Printing all elements in reverse order
    for (auto it = v.rbegin(); it != v.rend(); ++it)
        cout << *it << " ";
    return 0;
}

Output
56 15 11 9 

Example 2: Modifying Elements in Reverse Order

C++
// C++ program to modify vector elements in reverse
// using vector::rbegin() and vector::rend()
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {9, 11, 15, 56};

    // Doubling the values in reverse order
    for (auto it = v.rbegin(); it != v.rend(); ++it)
        *it *= 2;
  
    for (int i : v)
        cout << i << " ";
    return 0;
}

Output
18 22 30 112 

Difference Between vector::rbegin() and vector::rend()

The following table lists some primary differences between the vector::rbegin() and vector::rend() methods:

vector::rbegin()vector::rend()
Returns a reverse iterator to the last element in the vector.Returns a reverse iterator to one element before the first element.

Syntax: v.rbegin();

Syntax: v.rend();

We can dereference rbegin() as it points to a valid element.

We should not dereference rend() as it does not point to a valid element.


Next Article
Practice Tags :

Similar Reads