In C++11, std::unordered_set::cend() returns a read-only iterator to the position after the last value. Like end(), it is only a marker and must never be dereferenced.
It is the partner of cbegin(). Together they describe the whole unordered_set for loops and algorithms that must not change it. 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 cend with empty brackets. Use it as the end of a read-only loop.
C++
for (auto p = s.cbegin(); p != s.cend(); ++p) {
// read *p
}
auto n = std::distance(s.cbegin(), s.cend());
Call
What it does
s.cend()
Returns a const_iterator one past the last value, even when the unordered_set is not const.
You can compare a const_iterator with a normal iterator. So s.find(v) == s.cend() works even though find() returns an iterator.
Library declarations
C++
const_iterator cend() const noexcept; // C++11
Parameters
Parameter
Description
None
cend() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
const_iterator
A read-only iterator to the position after the last value. It equals cbegin() for an empty unordered_set.
C++ Examples
Example 1 — cend() to Test find()
Mix an iterator from find() with cend() in one comparison.
C++
#include <iostream>
#include <string>
#include <unordered_set>
int main() {
std::unordered_set<std::string> ids = {"ann", "ben"};
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;
}
Output
ben found: true
cy found: false
How It Works
find() on a non-const unordered_set returns a normal iterator, while cend() is a const_iterator.
The two types can be compared with !=. For "ben" the iterator points at a real value, so it differs from cend().
For "cy", find() returns the end position, which equals cend(), so the answer is false.
Example 2 — cend() with std::distance
Count the values by measuring the distance between cbegin() and cend().
C++
#include <iostream>
#include <iterator>
#include <unordered_set>
int main() {
std::unordered_set<char> s = {'a', 'b', 'c', 'd'};
auto n = std::distance(s.cbegin(), s.cend());
std::cout << "distance: " << n << '\n';
std::cout << "size: " << s.size() << '\n';
return 0;
}
Output
distance: 4
size: 4
How It Works
std::distance(first, last) counts how many steps lead from first to last.
From cbegin() to cend() it takes four steps, one for each value. An unordered_set iterator can only step one at a time, so this is a slow walk.
The result matches size(), which is the fast and correct way to count. The example only shows what the two markers mean.
Example 3 — cend() on an Empty Set
Show that cbegin() equals cend() when there is nothing to walk.