In C++11, std::unordered_set::cbegin() returns a read-only iterator (a const_iterator) to one value of the unordered_set. Which value comes first is unspecified.
The c stands for const. Through this iterator you can read *it, but the compiler stops you from changing it. On an unordered_set every element is already const, so cbegin() mainly makes the intent clear. Include <unordered_set> to use it.
This is a member of std::unordered_set<T>, a hash table of unique values. Find, insert, and erase are usually average constant time. Unlike std::set, values are not sorted and iteration order is unspecified. Elements are const, so you cannot change a stored value through an iterator. There is no operator[] and no at(). Prefer find() or contains() to look up a value, and call reserve() before many inserts.
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, order unspecified
}
Call
What it does
s.cbegin()
Returns a const_iterator to one value, even when the unordered_set itself is not const.
cbegin() says in your code that the loop will not change the unordered_set. On a const unordered_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 one value. It equals cend() for an empty unordered_set.
C++ Examples
Example 1 — cbegin() in a Loop
Add every number with a loop that cannot change the unordered_set.
C++
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_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';
std::cout << "size: " << scores.size() << '\n';
return 0;
}
Output
total: 35
size: 3
How It Works
The loop starts at cbegin() and stops at cend(). Visit order does not matter for a sum.
Each step adds the current value. 3 + 12 + 20 is 35.
The unordered_set was only read, so size() stays 3.
Example 2 — cbegin() Walk Versus contains()
Count how many wanted tags exist by walking with cbegin().
C++
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
int main() {
std::unordered_set<std::string> tags = {"red", "blue", "green"};
std::vector<std::string> want = {"blue", "yellow", "red"};
int hits = 0;
for (const std::string& w : want) {
for (auto it = tags.cbegin(); it != tags.cend(); ++it) {
if (*it == w) {
++hits;
break;
}
}
}
std::cout << "hits: " << hits << '\n';
std::cout << "contains blue: " << std::boolalpha << tags.contains("blue") << '\n';
return 0;
}
Output
hits: 2
contains blue: true
How It Works
The outer loop checks three wanted tags. The inner cbegin() walk looks for each one.
"blue" and "red" are present, so hits becomes 2. "yellow" is missing.
contains() (C++20) answers the same question faster. The walk only shows a read-only iterator in action.
Example 3 — cbegin() Compared with begin()
Show that cbegin() always gives a const_iterator.
C++
#include <iostream>
#include <type_traits>
#include <unordered_set>
int main() {
std::unordered_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::unordered_set<int>::iterator>::value << '\n';
std::cout << "cbegin() is const_iterator: " << std::is_same<B, std::unordered_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 unordered_set.
std::is_same compares each type with iterator and const_iterator. This is checked at compile time.
Both are true. On an unordered_set the element is const either way, but cbegin() makes the read-only intent obvious.
Frequently Asked Questions
A const_iterator to one value. You can read through it but not modify the element. Order is unspecified.
On a non-const unordered_set begin() returns iterator and cbegin() returns const_iterator. Elements stay read-only either way.
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.