C++ std::unordered_set cend() Function

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

Definition

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());
CallWhat 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

ParameterDescription
Nonecend() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
const_iteratorA 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;
}

How It Works

  1. find() on a non-const unordered_set returns a normal iterator, while cend() is a const_iterator.
  2. The two types can be compared with !=. For "ben" the iterator points at a real value, so it differs from cend().
  3. 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;
}

How It Works

  1. std::distance(first, last) counts how many steps lead from first to last.
  2. 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.
  3. 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.

C++
#include <iostream>
#include <unordered_set>

int main() {
    std::unordered_set<int> s;
    std::cout << std::boolalpha;
    std::cout << "empty: " << (s.cbegin() == s.cend()) << '\n';

    s.insert(7);
    std::cout << "with one value: " << (s.cbegin() == s.cend()) << '\n';
    std::cout << "size: " << s.size() << '\n';
    return 0;
}

How It Works

  1. With no values, cbegin() and cend() are the same position.
  2. After insert(7), cbegin() points at that value and cend() is one past it, so they differ.
  3. The size is 1. Never dereference cend().

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 unordered_set.
Yes. An iterator and a const_iterator of the same unordered_set can be compared with == and !=.
No. It does not point at a value. Doing so is undefined behavior.
C++11.

Next: clear()

Keep going through the std::unordered_set functions. Remove every value so the unordered_set becomes empty.

clear() 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.

12 people found this page helpful