In C++, std::deque::crbegin() returns a const_reverse_iterator that points to the last element of the deque. Moving it with ++ goes backwards, towards the first element.
The letters cr mean const reverse: you walk from back to front, and you can only read the values. 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 crbegin with empty brackets. It takes no arguments.
C++
auto it = d.crbegin();
int last = *it;
Call
What it does
d.crbegin()
Returns a read-only reverse iterator that starts on the last element.
*d.crbegin()
Reads the last element. It gives the same value as d.back().
If you need to change elements while walking backwards, use rbegin() instead.
Library declarations
C++
const_reverse_iterator crbegin() const noexcept;
Parameters
Parameter
Description
None
crbegin() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
Const reverse iterator
A const_reverse_iterator to the last element. ++it moves towards the front.
Empty deque
For an empty deque, crbegin() equals crend().
C++ Examples
Example 1 — Print a Deque Backwards
A reverse loop starts at crbegin() and ends at crend().
C++
#include <deque>
#include <iostream>
int main() {
std::deque<int> d = {1, 2, 3, 4};
for (auto it = d.crbegin(); it != d.crend(); ++it) {
std::cout << *it << ' ';
}
std::cout << '\n';
return 0;
}
Output
4 3 2 1
How It Works
crbegin() starts on 4, the last element.
Each ++it moves one step towards the front, so the order is 4 3 2 1.
The deque itself is not changed. Only the walking direction is reversed.
Example 2 — *crbegin() Equals back()
The first reverse element is the same as the last normal element.
A reverse iterator starts at the back of the deque, so crbegin() points to 9.
back() returns the same element directly as a reference.
Use back() when you want one value. Use crbegin() when you want to walk or pass a range to an algorithm.
Example 3 — Search from the Back with crbegin()
Find the last match of a value instead of the first one.
C++
#include <algorithm>
#include <deque>
#include <iostream>
#include <iterator>
int main() {
std::deque<int> d = {5, 8, 5, 9};
auto it = std::find(d.crbegin(), d.crend(), 5);
std::size_t fromBack = std::distance(d.crbegin(), it);
std::cout << "last 5 is " << fromBack << " place(s) from the back\n";
std::cout << "forward index: " << (d.size() - 1 - fromBack) << '\n';
return 0;
}
Output
last 5 is 1 place(s) from the back
forward index: 2
How It Works
Searching with reverse iterators checks 9 first, then 5, so it finds the match that is closest to the back.
std::distance(d.crbegin(), it) counts the steps taken from the back. Here it is 1.
size() - 1 - fromBack turns that into a normal index: 4 - 1 - 1 = 2. The last 5 is at index 2.
Frequently Asked Questions
It returns a const_reverse_iterator that points to the last element of the deque. Incrementing it moves towards the first element.
rbegin() can return a changeable reverse iterator. crbegin() always returns a const_reverse_iterator, so you can read the elements but not modify them.
No. The deque is not changed. Only the direction you walk through it is reversed.
They point to the same element, but they are different types. crbegin() moves backwards with ++, while cend() - 1 is a normal iterator that moves forwards with ++.
crend(). It marks the place just before the first element, so it is the end marker for reverse walking.