C++ std::map cend() Function

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

Definition

In C++11, std::map::cend() returns a read-only iterator to the position after the last pair. Like end(), it is only a marker and must never be dereferenced.

It is the partner of cbegin(). Together they describe the whole map for loops and algorithms that 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 cend with empty brackets. Use it as the end of a read-only loop.

C++
for (auto p = m.cbegin(); p != m.cend(); ++p) {
    // read p->first and p->second
}
n = std::distance(m.cbegin(), m.cend());
CallWhat it does
m.cend()Returns a const_iterator one past the last pair, even when the map is not const.

You can compare a const_iterator with a normal iterator. So m.find(k) == m.cend() works even though find() returns an iterator.

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

Parameters

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

Return Value

Return ValueDescription
const_iteratorA read-only iterator to the position after the last pair. It equals cbegin() for an empty map.

C++ Examples

Example 1 — cend() Ends a Read-Only Loop

Walk the map from cbegin() to cend() and print each pair.

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

int main() {
    std::map<int, std::string> day = {{3, "Wed"}, {1, "Mon"}, {2, "Tue"}};

    for (auto it = day.cbegin(); it != day.cend(); ++it) {
        std::cout << it->first << " = " << it->second << '\n';
    }
    return 0;
}

How It Works

  1. The map sorts the keys, so cbegin() points at key 1, not at the first one that was written.
  2. The loop keeps going while the iterator differs from cend(). After the pair for key 3, the iterator reaches cend() and the loop stops.
  3. Because both ends are const_iterators, the loop can read every pair but cannot change any of them.

Example 2 — cend() to Test find()

Mix an iterator from find() with cend() in one comparison.

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

int main() {
    std::map<std::string, int> ids = {{"ann", 1}, {"ben", 2}};

    auto a = ids.find("ben");
    auto b = ids.find("cy");

    std::cout << std::boolalpha;
    std::cout << "ben found: " << (a != ids.cend()) << '\n';
    std::cout << "cy found: " << (b != ids.cend()) << '\n';
    return 0;
}

How It Works

  1. find() on a non-const map returns a normal iterator, while cend() is a const_iterator.
  2. The two types can be compared with !=. For "ben" the iterator points at a real pair, so it differs from cend().
  3. For "cy", find() returns the end position, which equals cend(), so the answer is false.

Example 3 — cend() with std::distance

Count the pairs by measuring the distance between cbegin() and cend().

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

int main() {
    std::map<char, int> m = {{'a', 1}, {'b', 2}, {'c', 3}, {'d', 4}};

    auto n = std::distance(m.cbegin(), m.cend());

    std::cout << "distance: " << n << '\n';
    std::cout << "size: " << m.size() << '\n';
    return 0;
}

How It Works

  1. std::distance(first, last) counts how many steps lead from first to last.
  2. From cbegin() to cend() it takes four steps, one for each pair. A map iterator can only step one at a time, so this is a slow walk.
  3. The result matches size(), which is the fast and correct way to count. The example only shows what the two markers mean.

Frequently Asked Questions

A const_iterator to the position after the last pair. It is a marker and not a real pair.
Both mark the end. cend() always returns a const_iterator, so it fits loops that must not change the map.
Yes. An iterator and a const_iterator of the same map can be compared with == and !=.
No. It does not point at a pair. Doing so is undefined behavior.
C++11.

Next: clear()

Keep going through the std::map functions. Remove every pair from the map.

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

10 people found this page helpful