C++ std::map cbegin() Function

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

Definition

In C++11, std::map::cbegin() returns a read-only iterator (a const_iterator) to the first pair of the map. The first pair is the one with the smallest key.

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 <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 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 map itself is not const.

cbegin() says in your code that the loop will not change the 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

Get a read-only iterator to the first pair and print it.

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

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

    auto it = price.cbegin();
    std::cout << it->first << " costs " << it->second << '\n';
    return 0;
}

How It Works

  1. The keys sort as cake, milk, tea. The first one is cake.
  2. cbegin() returns a const_iterator to that pair. Reading it->first and it->second works as normal.
  3. The line it->second = 9 would be a compile error here, so the program cannot change the price by mistake.

Example 2 — cbegin() in a Loop

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

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

int main() {
    std::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(), which points at bag because the keys are sorted.
  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 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 <map>
#include <type_traits>

int main() {
    std::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::map<int, int>::iterator>::value << '\n';
    std::cout << "cbegin() is const_iterator: " << std::is_same<B, std::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 smallest key). You can read through it but not modify the pair.
On a non-const 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::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.

9 people found this page helpful