Home C++ Deques Functions rbegin() C++ std::deque rbegin() Function Definition In C++, std::deque::rbegin() returns a reverse iterator that points to the last element of the deque. Moving it with ++ goes backwards, towards the first element.
Use it with rend() to walk through the deque from back to front. It can also change elements. Include <deque> to use it.
This is a member of std::deque<T>, a double-ended queue . A deque is fast at adding and removing at both ends, and it allows access by index like std::vector. Unlike a vector, its elements are stored in several blocks, not in one contiguous block.
Syntax Call rbegin with empty brackets. It takes no arguments.
auto it = d.rbegin();
*it = 99; // changes the last element Call What it does d.rbegin()Returns a reverse iterator that starts on the last element. *d.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 deque it is a const_reverse_iterator. Empty deque For an empty deque, 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 <deque>
#include <iostream>
int main() {
std::deque<int> d = {10, 20, 30};
for (auto it = d.rbegin(); it != d.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 deque is not reordered. Example 2 — Change Values Through rbegin() A reverse iterator that is not const can write to the elements.
#include <deque>
#include <iostream>
int main() {
std::deque<int> d = {1, 2, 3};
auto it = d.rbegin();
*it = 99;
++it;
*it += 1;
for (int n : d) {
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 deque is now 1 3 99.Example 3 — Make a Reversed Copy Pass the reverse iterator pair to a constructor to build a reversed deque.
#include <deque>
#include <iostream>
int main() {
std::deque<int> d = {1, 2, 3, 4};
std::deque<int> rev(d.rbegin(), d.rend());
for (int n : d) {
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 deque d is not changed. It is still 1 2 3 4. Frequently Asked Questions What does std::deque::rbegin() return? It returns a reverse iterator that points to the last element of the deque. 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 deque? No. The deque stays the same. Only the direction of the walk changes.
Can I change elements with rbegin()? Yes, on a non-const deque. 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::deque.
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