In C++11, std::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 set for loops and algorithms that must not change it. 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 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
}
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 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 set.
C++ Examples
Example 1 — cend() Ends a Read-Only Loop
Walk the set from cbegin() to cend() and print each value.
C++
#include <iostream>
#include <set>
#include <string>
int main() {
std::set<std::string> day = {"Wed", "Mon", "Tue"};
for (auto it = day.cbegin(); it != day.cend(); ++it) {
std::cout << *it << '\n';
}
return 0;
}
Output
Mon
Tue
Wed
How It Works
The set sorts the strings, so cbegin() points at Mon, not at the first one that was written.
The loop keeps going while the iterator differs from cend(). After Wed, the iterator reaches cend() and the loop stops.
Because both ends are const_iterators, the loop can read every value 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 <set>
#include <string>
int main() {
std::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 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 3 — cend() with std::distance
Count the values by measuring the distance between cbegin() and cend().
C++
#include <iostream>
#include <iterator>
#include <set>
int main() {
std::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. A 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.
Frequently Asked Questions
A const_iterator to the position after the last value. It is a marker and not a real value.
Both mark the end. cend() always returns a const_iterator, so it fits loops that must not change the set.
Yes. An iterator and a const_iterator of the same set can be compared with == and !=.
No. It does not point at a value. Doing so is undefined behavior.