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
}
Call
What 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
Parameter
Description
None
cbegin() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
const_iterator
A 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;
}
Output
first: cake
How It Works
The values sort as cake, milk, tea. The first one is cake.
cbegin() returns a const_iterator to that value. Reading *it works as normal.
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;
}
Output
total: 35
How It Works
The loop starts at cbegin(), which points at the smallest score, 3.
Each step adds the current value, then ++it moves on. The running total goes 3, then 15, then 35.
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;
}
Output
begin() is iterator: true
cbegin() is const_iterator: true
How It Works
decltype(s.begin()) gives the exact type that begin() returns for this non-const set.
std::is_same compares each type with iterator and const_iterator. This is checked at compile time.
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.