Home C++ Maps Functions crbegin() C++ std::map crbegin() Function Definition In C++11, std::map::crbegin() returns a read-only reverse iterator (const_reverse_iterator) that starts at the last pair , the one with the biggest key .
The letters c and r stand for const and reverse . It is the right choice when you walk a map backwards and must not change it. Include <map> to use it.
This is a member of std::map<Key, T>, a sorted container of key and value pairs . Each key is unique and the pairs are always kept in key order, so a map can find, insert, and erase a key in logarithmic time. Read a value with at() or find(). The m[key] operator also works, but it adds a default value when the key is missing.
Syntax Call crbegin with empty brackets. Pair it with crend().
auto it = m.crbegin(); // biggest key, read-only
for (auto p = m.crbegin(); p != m.crend(); ++p) {
// read only, biggest key first
} Call What it does m.crbegin()Returns a const_reverse_iterator to the last pair, even when the map is not const.
A function that takes const std::map& gets read-only iterators anyway. crbegin() is most useful when you hold a non-const map but want to promise that this loop only reads.
Library declarations const_reverse_iterator crbegin() const noexcept; // C++11 Parameters Parameter Description None crbegin() takes no parameters. Call it with empty brackets.
Return Value Return Value Description const_reverse_iteratorA read-only reverse iterator to the last pair. It equals crend() for an empty map.
C++ Examples 1. Read the biggest key 2. Reverse print a const map 3. Copy keys in descending order Example 1 — crbegin() Reads the Last Pair Print the biggest key and its value using a read-only reverse iterator.
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> height = {{"Ann", 160}, {"Zoe", 172}, {"Bob", 181}};
if (!height.empty()) {
auto it = height.crbegin();
std::cout << it->first << " " << it->second << '\n';
}
return 0;
} How It Works The keys sort as Ann, Bob, Zoe. The last pair in that order is Zoe. crbegin() starts at that pair, so it->first is Zoe and it->second is 172. It is the biggest key , not the biggest height.The result is read-only, so the program cannot change the data by accident. Example 2 — crbegin() in a Function Pass a const map to a function that prints the pairs in reverse order.
#include <iostream>
#include <map>
#include <string>
void printReverse(const std::map<int, std::string>& m) {
for (auto it = m.crbegin(); it != m.crend(); ++it) {
std::cout << it->first << "=" << it->second << ' ';
}
std::cout << '\n';
}
int main() {
std::map<int, std::string> m = {{1, "x"}, {2, "y"}, {3, "z"}};
printReverse(m);
return 0;
} How It Works The function takes a const reference, so it promises not to change the map. Calling crbegin() states the same promise in the loop. The iterator starts at key 3 and each ++it moves toward key 1. The loop stops at crend(), so the output is 3=z 2=y 1=x, in reverse key order. Example 3 — crbegin() to Copy Keys Backwards Collect the keys of a map into a vector, from the biggest to the smallest.
#include <iostream>
#include <map>
#include <vector>
int main() {
std::map<int, double> m = {{2, 0.5}, {7, 1.5}, {4, 2.5}};
std::vector<int> keys;
for (auto it = m.crbegin(); it != m.crend(); ++it) {
keys.push_back(it->first);
}
for (int k : keys) {
std::cout << k << ' ';
}
std::cout << '\n';
return 0;
} How It Works The map stores keys in the order 2, 4, 7. The reverse walk visits them as 7, 4, 2. Each step pushes it->first into the vector. Only the key is copied; the map and its values are untouched. The vector now holds the keys in descending order, which is what the second loop prints. Frequently Asked Questions What does std::map::crbegin() return? A const_reverse_iterator that points to the last pair of the map, which has the biggest key. It is read-only.
What is the difference between rbegin() and crbegin()? rbegin() on a non-const map allows changing values. crbegin() always returns a read-only iterator.
Which C++ version has crbegin()? C++11.
Can I erase through a const_reverse_iterator? Not directly. Convert it to a normal iterator with std::next(it).base() and pass that to erase().
Does crbegin() work on an empty map? Yes. It equals crend(), but you must not dereference it.
Next: crend() Keep going through the std::map functions. Get a read-only reverse iterator before the first pair (C++11).
crend() 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