Home C++ Vectors Functions rbegin() C++ std::vector rbegin() Function Definition In C++, std::vector::rbegin() returns a reverse iterator that points to the last element of the vector. Moving it with ++ goes backwards, towards the first element.
Use it with rend() to walk through the vector from back to front. It can also change elements. Include <vector> to use it.
This is a member of std::vector<T>, a dynamic contiguous array . Elements sit in one block, so v[i] and data() are fast. Growing past capacity() reallocates and can invalidate iterators, references, and pointers. Prefer push_back / pop_back at the end; middle inserts shift elements.
Syntax Call rbegin with empty brackets. It takes no arguments.
auto it = v.rbegin();
*it = 99; // changes the last element Call What it does v.rbegin()Returns a reverse iterator that starts on the last element. *v.rbegin() = 99Changes the last element through the reverse iterator.
For a read-only version, use crbegin().
Library declarations reverse_iterator rbegin() noexcept;
const_reverse_iterator rbegin() const noexcept; Parameters Parameter Description None rbegin() takes no parameters. Call it with empty brackets.
Return Value Return Value Description Reverse iterator A reverse_iterator to the last element. On a const vector it is a const_reverse_iterator. Empty vector For an empty vector, rbegin() equals rend().
C++ Examples 1. Reverse loop 2. Modify 3. Reversed copy Example 1 — Loop Backwards with rbegin() A reverse loop starts at rbegin() and ends at rend().
#include <vector>
#include <iostream>
int main() {
std::vector<int> v = {10, 20, 30};
for (auto it = v.rbegin(); it != v.rend(); ++it) {
std::cout << *it << ' ';
}
std::cout << '\n';
return 0;
} How It Works rbegin() starts on 30, the last element.Each ++it moves one place towards the front, so the order is 30 20 10. The loop stops at rend(). The vector is not reordered. Example 2 — Change Values Through rbegin() A reverse iterator that is not const can write to the elements.
#include <vector>
#include <iostream>
int main() {
std::vector<int> v = {1, 2, 3};
auto it = v.rbegin();
*it = 99;
++it;
*it += 1;
for (int n : v) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
} How It Works *it = 99 replaces the last element, which was 3.++it moves the reverse iterator one place towards the front, onto 2.*it += 1 changes that element to 3. The vector is now 1 3 99.Example 3 — Make a Reversed Copy Pass the reverse iterator pair to a constructor to build a reversed vector.
#include <vector>
#include <iostream>
int main() {
std::vector<int> v = {1, 2, 3, 4};
std::vector<int> rev(v.rbegin(), v.rend());
for (int n : v) {
std::cout << n << ' ';
}
std::cout << '\n';
for (int n : rev) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
} How It Works rev(d.rbegin(), d.rend()) copies the elements in the order that the reverse iterators visit them.That order is back to front, so rev becomes 4 3 2 1. The original vector d is not changed. It is still 1 2 3 4. Frequently Asked Questions What does std::vector::rbegin() return? It returns a reverse iterator that points to the last element of the vector. Incrementing it moves towards the first element.
What is the difference between rbegin() and end()? end() points after the last element and cannot be read. rbegin() points to the last element and can be read. They also move in opposite directions.
Does rbegin() reverse the vector? No. The vector stays the same. Only the direction of the walk changes.
Can I change elements with rbegin()? Yes, on a non-const vector. Use crbegin() if you want a read-only reverse iterator.
What stops a reverse loop? rend(). It marks the place just before the first element, so it is the end marker for reverse walking.
Next: rend() Get the reverse end marker, just before the first element with std::vector.
rend() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
14 people found this page helpful
Helpful Share Copy link Suggestion