C++ std::map crbegin() Function

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

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().

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

Parameters

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

Return Value

Return ValueDescription
const_reverse_iteratorA read-only reverse iterator to the last pair. It equals crend() for an empty map.

C++ Examples

Example 1 — crbegin() Reads the Last Pair

Print the biggest key and its value using a read-only reverse iterator.

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

  1. The keys sort as Ann, Bob, Zoe. The last pair in that order is Zoe.
  2. crbegin() starts at that pair, so it->first is Zoe and it->second is 172. It is the biggest key, not the biggest height.
  3. 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.

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

  1. The function takes a const reference, so it promises not to change the map. Calling crbegin() states the same promise in the loop.
  2. The iterator starts at key 3 and each ++it moves toward key 1.
  3. 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.

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

  1. The map stores keys in the order 2, 4, 7. The reverse walk visits them as 7, 4, 2.
  2. Each step pushes it->first into the vector. Only the key is copied; the map and its values are untouched.
  3. The vector now holds the keys in descending order, which is what the second loop prints.

Frequently Asked Questions

A const_reverse_iterator that points to the last pair of the map, which has the biggest key. It is read-only.
rbegin() on a non-const map allows changing values. crbegin() always returns a read-only iterator.
C++11.
Not directly. Convert it to a normal iterator with std::next(it).base() and pass that to erase().
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

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.

14 people found this page helpful