Last Updated : 11 Jul, 2025
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;
}
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() MethodThe std::vector::rbegin() method is used to retrieve a vector::reverse_iterator pointing to the last element (reverse beginning) of the std::vector container.
Syntaxv.rbegin()
Parameters
Return Value
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.
Syntaxv.rend()
Parameters
Return Value
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 ReverseWe 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;
}
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;
}
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4