C++ std::map crend() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::map

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.

C++
for (auto p = m.crbegin(); p != m.crend(); ++p) {
    // read only, biggest key first
}
auto n = std::distance(m.crbegin(), m.crend());
CallWhat 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
C++
const_reverse_iterator crend() const noexcept;   // C++11

Parameters

ParameterDescription
Nonecrend() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
const_reverse_iteratorA read-only reverse iterator to the position before the first pair. It equals crbegin() for an empty map.

C++ Examples

Example 1 — crend() Ends a Reverse Loop

Print the pairs from the last to the first with iterators that cannot change anything.

C++
#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

  1. crbegin() starts at the pair c, which has the biggest key.
  2. Every ++it moves one pair toward the front: c, then b, then a.
  3. 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.

C++
#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

  1. std::distance counts the steps from the first iterator to the second.
  2. Going from the last pair back to the position before the first pair takes one step per pair, so there are 3 steps.
  3. 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.

C++
#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;
}

How It Works

  1. std::find_if checks the pairs one by one from crbegin() toward crend(), so day 4 is tested first.
  2. 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.
  3. 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

A const_reverse_iterator to the position before the first pair. It marks the end of a read-only reverse walk.
No. It does not point at a real pair.
Both mark the end of a reverse walk. crend() always returns a const_reverse_iterator, so it is read-only.
It means nothing matched. For example, std::find_if returns crend() when no pair satisfies the test.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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