C++ std::unordered_map cbegin() Function

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

Definition

In C++11, std::unordered_map::cbegin() returns a read-only iterator (a const_iterator) to the first pair of the unordered_map. The first pair is the one with the first pair in iteration order.

The c stands for const. Through this iterator you can read it->first and it->second, but the compiler stops you from changing them. Include <unordered_map> to use it.

This is a member of std::unordered_map<Key, T>, a hash table of key and value pairs. Each key is unique. Find, insert, and erase are usually average constant time. Unlike std::map, keys are not sorted and iteration order is unspecified. 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 cbegin with empty brackets. Pair it with cend().

C++
auto it = m.cbegin();                 // const_iterator
for (auto p = m.cbegin(); p != m.cend(); ++p) {
    std::cout << p->first;            // read only
}
CallWhat it does
m.cbegin()Returns a const_iterator to the first pair, even when the unordered_map itself is not const.

cbegin() says in your code that the loop will not change the unordered_map. On a const map, begin() already returns a const_iterator, so both give the same type.

Library declarations
C++
const_iterator cbegin() const noexcept;   // C++11

Parameters

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

Return Value

Return ValueDescription
const_iteratorA read-only iterator to the first pair. It equals cend() for an empty map.

C++ Examples

Example 1 — cbegin() Reads Without Changing

Show that cbegin is valid on a non-empty map and that values can still be read by key.

C++
#include <iostream>
#include <string>
#include <unordered_map>

int main() {
    std::unordered_map<std::string, int> price = {{"tea", 3}, {"cake", 5}, {"milk", 2}};

    std::cout << std::boolalpha;
    std::cout << "cbegin != cend: " << (price.cbegin() != price.cend()) << '\n';
    std::cout << "cake: " << price.at("cake") << '\n';
    std::cout << "tea: " << price.at("tea") << '\n';
    return 0;
}

How It Works

  1. cbegin() != cend() is true because the unordered_map is not empty.
  2. Which pair cbegin() points at is unspecified, so the demo reads known keys with at().
  3. Assigning through a const_iterator from cbegin() would not compile, which is the point of the const versions.

Example 2 — cbegin() in a Loop

Add up all values with a loop that is not allowed to change them.

C++
#include <iostream>
#include <unordered_map>
#include <string>

int main() {
    std::unordered_map<std::string, int> cart = {{"pen", 3}, {"book", 12}, {"bag", 20}};

    int total = 0;
    for (auto it = cart.cbegin(); it != cart.cend(); ++it) {
        total += it->second;
    }

    std::cout << "total: " << total << '\n';
    return 0;
}

How It Works

  1. The loop starts at cbegin() and visits every pair. The first key is unspecified.
  2. Each step adds the value of the current pair, then ++it moves on. The running total goes 20, then 32, then 35.
  3. The loop ends when it equals cend(). The unordered_map was only read, so it is unchanged.

Example 3 — cbegin() Compared with begin()

Show that begin() on a non-const map gives a changeable iterator, and cbegin() does not.

C++
#include <iostream>
#include <unordered_map>
#include <type_traits>

int main() {
    std::unordered_map<int, int> m = {{1, 10}};

    using A = decltype(m.begin());
    using B = decltype(m.cbegin());

    std::cout << std::boolalpha;
    std::cout << "begin() is iterator: " << std::is_same<A, std::unordered_map<int, int>::iterator>::value << '\n';
    std::cout << "cbegin() is const_iterator: " << std::is_same<B, std::unordered_map<int, int>::const_iterator>::value << '\n';
    return 0;
}

How It Works

  1. decltype(m.begin()) gives the exact type that begin() returns for this non-const map.
  2. std::is_same compares each type with iterator and const_iterator. This is checked at compile time.
  3. Both are true. So with begin() you may change values, and with cbegin() you may only read.

Frequently Asked Questions

A const_iterator to the first pair (the first pair in iteration order). You can read through it but not modify the pair.
On a non-const unordered_map begin() returns an iterator that can change values. cbegin() always returns a const_iterator, so it is read-only.
C++11.
Use it when a loop only reads. The compiler then catches any accidental change.
Yes. It equals cend(), but you must not dereference it.

Next: cend()

Keep going through the std::unordered_map functions. Get a read-only iterator past the last pair (C++11).

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

12 people found this page helpful