Home C++ Maps Functions crend() C++ std::map crend() Function Definition In C++11, std::map::crend() returns a read-only reverse iterator to the position before the first pair . It ends a reverse walk that began at crbegin().
It is a marker, not a pair, so you must never dereference it. Algorithms such as std::find_if use it as the “nothing found” result. 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 crend with empty brackets. Use it as the stop value of a read-only reverse loop.
for (auto p = m.crbegin(); p != m.crend(); ++p) {
// read only, biggest key first
}
auto n = std::distance(m.crbegin(), m.crend()); Call What it does m.crend()Returns a const_reverse_iterator one step before the first pair, even when the map is not const.
crbegin() and crend() are always used together. On an empty map they are equal, so any loop or algorithm over them does nothing.
Library declarations const_reverse_iterator crend() const noexcept; // C++11 Parameters Parameter Description None crend() takes no parameters. Call it with empty brackets.
Return Value Return Value Description const_reverse_iteratorA read-only reverse iterator to the position before the first pair. It equals crbegin() for an empty map.
Important
Never dereference crend() . When std::find_if(m.crbegin(), m.crend(), ...) returns crend(), it means no pair matched. Check for that before you read the result.
C++ Examples 1. Read-only reverse loop 2. Measure the reverse range 3. Search from the back Example 1 — crend() Ends a Reverse Loop Print the pairs from the last to the first with iterators that cannot change anything.
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> m = {{"a", 1}, {"b", 2}, {"c", 3}};
for (auto it = m.crbegin(); it != m.crend(); ++it) {
std::cout << it->first << it->second << ' ';
}
std::cout << '\n';
return 0;
} How It Works crbegin() starts at the pair c, which has the biggest key.Every ++it moves one pair toward the front: c, then b, then a. After a, the next step reaches crend(), which is before the first pair. The loop stops and nothing was changed. Example 2 — crend() with std::distance The distance from crbegin() to crend() counts every pair once.
#include <iostream>
#include <iterator>
#include <map>
#include <string>
int main() {
std::map<int, std::string> m = {{4, "d"}, {2, "b"}, {3, "c"}};
auto steps = std::distance(m.crbegin(), m.crend());
std::cout << "steps: " << steps << '\n';
std::cout << "size: " << m.size() << '\n';
return 0;
} How It Works std::distance counts the steps from the first iterator to the second.Going from the last pair back to the position before the first pair takes one step per pair, so there are 3 steps. The number equals size(). Use size() in real code: it is instant, while distance has to walk through the whole map. Example 3 — crend() with std::find_if Find the pair with the biggest key whose value is below a limit, searching from the back.
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<int, int> temp = {{1, 30}, {2, 12}, {3, 41}, {4, 15}};
auto it = std::find_if(temp.crbegin(), temp.crend(),
[](const std::pair<const int, int>& p) { return p.second < 20; });
if (it == temp.crend()) {
std::cout << "none\n";
} else {
std::cout << "last day under 20: day " << it->first << " (" << it->second << ")\n";
}
return 0;
} last day under 20: day 4 (15) How It Works std::find_if checks the pairs one by one from crbegin() toward crend(), so day 4 is tested first.The lambda is true when the value is below 20. Day 4 has 15, so the search stops there and returns an iterator to it. If no pair matched, the algorithm would return crend(). The program checks for that first, so it never reads from the marker. Frequently Asked Questions What does std::map::crend() return? A const_reverse_iterator to the position before the first pair. It marks the end of a read-only reverse walk.
Can I dereference crend()? No. It does not point at a real pair.
What is the difference between rend() and crend()? Both mark the end of a reverse walk. crend() always returns a const_reverse_iterator, so it is read-only.
What does an algorithm return when it reaches crend()? It means nothing matched. For example, std::find_if returns crend() when no pair satisfies the test.
Which C++ version has crend()? C++11.
Next: emplace() Keep going through the std::map functions. Build a new pair in place from its constructor arguments (C++11).
emplace() 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.
15 people found this page helpful
Helpful Share Copy link Suggestion