In C++11 and later, std::list::crbegin() returns a const reverse iterator that points at the last element of a std::list. The letters cr mean const reverse.
You can walk the list from back to front and read every element, but you cannot change any of them. Include <list> to use it.
This is a member of std::list<T>, a doubly linked list. Every element lives in its own node with a link to the previous and the next node, so a list can insert and erase anywhere quickly once you hold an iterator. It has no access by index, so l[i] does not exist.
Syntax
Call crbegin with empty brackets and pair it with crend().
C++
for (auto it = l.crbegin(); it != l.crend(); ++it) {
std::cout << *it; // read only
}
std::vector<int> v(l.crbegin(), l.crend()); // reversed copy
Call
What it does
l.crbegin()
Returns a const_reverse_iterator to the last element. On an empty list it equals crend().
rbegin() on a non-const list lets you write. crbegin() never does.
Library declarations
C++
const_reverse_iterator crbegin() const noexcept; // since C++11
Parameters
Parameter
Description
None
crbegin() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
const_reverse_iterator
A read-only reverse iterator that points at the last element.
C++ Examples
Example 1 — crbegin() and crend() in a Loop
Print a list from the last element to the first one, with read-only access.
C++
#include <iostream>
#include <list>
int main() {
std::list<char> l = {'a', 'b', 'c', 'd'};
for (auto it = l.crbegin(); it != l.crend(); ++it) {
std::cout << *it;
}
std::cout << '\n';
return 0;
}
Output
dcba
How It Works
l.crbegin() is a read-only iterator at the last node, which holds 'd'.
Each ++it moves one step toward the front: d, c, b, a.
The loop stops at l.crend(). The characters were only read, never changed.
Example 2 — crbegin() to Fill Another Container
Give the two iterators to a std::vector and it builds a reversed copy of the list.
C++
#include <iostream>
#include <list>
#include <vector>
int main() {
std::list<int> l = {10, 20, 30};
std::vector<int> v(l.crbegin(), l.crend());
for (int n : v) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
30 20 10
How It Works
The vector constructor copies every element from the first iterator up to the second one.
Because we pass crbegin() first, the copy starts with the last list element, 30.
The vector holds 30 20 10. The list l still reads 10 20 30, because we only looked at it.
Example 3 — crbegin() to Test a Palindrome
Compare the list from the front with the same list from the back, using std::equal().
C++
#include <algorithm>
#include <iostream>
#include <list>
int main() {
std::list<int> l = {1, 2, 3, 2, 1};
bool same = std::equal(l.cbegin(), l.cend(), l.crbegin());
std::cout << std::boolalpha;
std::cout << "palindrome: " << same << '\n';
return 0;
}
Output
palindrome: true
How It Works
std::equal() walks two sequences together and checks that matching elements are equal.
The first sequence goes from the front with cbegin(), and the second one goes from the back with crbegin().
The pairs are 1-1, 2-2, 3-3, 2-2, 1-1, so the result is true.
Frequently Asked Questions
A const reverse iterator that points at the last element. You can read through it, but not change the element.
rbegin() on a non-const list gives a reverse iterator that can change elements. crbegin() always gives a read-only reverse iterator.
C++11 and later.
Pass crbegin() and crend() to a constructor, for example std::vector<int> v(l.crbegin(), l.crend()); The list itself is not changed.
When the list is empty. There is nothing to visit, so a backwards loop does not run.