C++ std::set cbegin() Function

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

Definition

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

The c stands for const. Through this iterator you can read *it, but the compiler stops you from changing it. On a set every element is already const, so cbegin() mainly makes the intent clear. Include <set> to use it.

This is a member of std::set<T>, a sorted container of unique values. A set never keeps a duplicate and always holds its values in sorted order, so it can find, insert, and erase a value in logarithmic time. A set has no operator[] and no at(), because the value is its own key. Every element is const, so you cannot change a value through an iterator.

Syntax

Call cbegin with empty brackets. Pair it with cend().

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

cbegin() says in your code that the loop will not change the set. On a const set, 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 value. It equals cend() for an empty set.

C++ Examples

Example 1 — cbegin() Reads Without Changing

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

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

int main() {
    std::set<std::string> items = {"tea", "cake", "milk"};

    auto it = items.cbegin();
    std::cout << "first: " << *it << '\n';
    return 0;
}

How It Works

  1. The values sort as cake, milk, tea. The first one is cake.
  2. cbegin() returns a const_iterator to that value. Reading *it works as normal.
  3. Trying to assign through the iterator would be a compile error, so the program cannot change the set by mistake.

Example 2 — cbegin() in a Loop

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

C++
#include <iostream>
#include <set>

int main() {
    std::set<int> scores = {3, 12, 20};

    int total = 0;
    for (auto it = scores.cbegin(); it != scores.cend(); ++it) {
        total += *it;
    }

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

How It Works

  1. The loop starts at cbegin(), which points at the smallest score, 3.
  2. Each step adds the current value, then ++it moves on. The running total goes 3, then 15, then 35.
  3. The loop ends when it equals cend(). The set was only read, so it is unchanged.

Example 3 — cbegin() Compared with begin()

Show that cbegin() always gives a const_iterator.

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

int main() {
    std::set<int> s = {1};

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

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

How It Works

  1. decltype(s.begin()) gives the exact type that begin() returns for this non-const set.
  2. std::is_same compares each type with iterator and const_iterator. This is checked at compile time.
  3. Both are true. On a set the element is const either way, but cbegin() makes the read-only intent obvious.

Frequently Asked Questions

A const_iterator to the first value (the smallest). You can read through it but not modify the element.
On a non-const set begin() returns iterator and cbegin() returns const_iterator. For set elements both are still read-only.
C++11.
Use it when a loop only reads. The name documents that intent for readers of your code.
Yes. It equals cend(), but you must not dereference it.

Next: cend()

Keep going through the std::set functions. Get a read-only iterator past the last value (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.

8 people found this page helpful