In C++, std::vector::crbegin() returns a const_reverse_iterator that points to the last element of the vector. 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 <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 crbegin with empty brackets. It takes no arguments.
C++
auto it = v.crbegin();
int last = *it;
Call
What it does
v.crbegin()
Returns a read-only reverse iterator that starts on the last element.
*v.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 vector
For an empty vector, crbegin() equals crend().
C++ Examples
Example 1 — Print a Vector Backwards
A reverse loop starts at crbegin() and ends at crend().
C++
#include <vector>
#include <iostream>
int main() {
std::vector<int> v = {1, 2, 3, 4};
for (auto it = v.crbegin(); it != v.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 vector 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 vector, 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 <vector>
#include <iostream>
#include <iterator>
int main() {
std::vector<int> v = {5, 8, 5, 9};
auto it = std::find(v.crbegin(), v.crend(), 5);
std::size_t fromBack = std::distance(v.crbegin(), it);
std::cout << "last 5 is " << fromBack << " place(s) from the back\n";
std::cout << "forward index: " << (v.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 vector. 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 vector 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.