C++ std::set cend() Function

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

Definition

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

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 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;
}

How It Works

  1. The set sorts the strings, so cbegin() points at Mon, not at the first one that was written.
  2. The loop keeps going while the iterator differs from cend(). After Wed, the iterator reaches cend() and the loop stops.
  3. 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;
}

How It Works

  1. find() on a non-const 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 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;
}

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. A 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.

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.
C++11.

Next: clear()

Keep going through the std::set functions. Remove every value so the 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.

9 people found this page helpful