C++ std::set end() Function

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

Definition

In C++, std::set::end() returns an iterator to the position after the last value. It is a marker, not a real value, so you must never dereference it.

You use it to know when a loop is finished, and to test the result of find(), lower_bound(), and upper_bound(), which return end() when nothing matches. 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 end with empty brackets. Use it as the stop value in a loop or as a “not found” test.

C++
if (s.find("Zoe") == s.end()) {
    // not found
}
auto last = std::prev(s.end());      // the last value
CallWhat it does
s.end()Returns an iterator one past the last value. On a const set it returns a const_iterator.

To get the last value, write std::prev(s.end()) or use s.rbegin(). Do not write *s.end() and do not use s.end() - 1, because set iterators do not support subtraction.

Library declarations
C++
iterator end() noexcept;
const_iterator end() const noexcept;

Parameters

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

Return Value

Return ValueDescription
iteratorAn iterator to the position after the last value. It is equal to begin() if the set is empty.

C++ Examples

Example 1 — end() Means Not Found

Compare the result of find() with end().

C++
#include <iostream>
#include <set>
#include <string>

int main() {
    std::set<std::string> names = {"Mia", "Raj"};

    for (const std::string name : {"Raj", "Zoe"}) {
        auto it = names.find(name);
        if (it == names.end()) {
            std::cout << name << ": not found\n";
        } else {
            std::cout << name << ": " << *it << '\n';
        }
    }
    return 0;
}

How It Works

  1. find() returns an iterator. It cannot return nullptr, so end() is the agreed way to say “nothing found”.
  2. For "Raj", the iterator points at a real value, so it is not equal to end() and the value prints.
  3. For "Zoe", find() returns end(), so the first branch runs. The code never reads a value from end().

Example 2 — end() and std::prev()

Step back one position from end() to reach the last value, which is the biggest.

C++
#include <iostream>
#include <iterator>
#include <set>

int main() {
    std::set<int> s = {5, 1, 9};

    if (!s.empty()) {
        auto last = std::prev(s.end());
        std::cout << "last: " << *last << '\n';
    }
    return 0;
}

How It Works

  1. s.end() is one position after the last value, so reading it directly would be an error.
  2. std::prev(s.end()) moves one position back, onto the last value. That is the biggest value, 9.
  3. The !s.empty() check matters. On an empty set end() equals begin(), and stepping back would be undefined.

Example 3 — end() on an Empty Set

On an empty set begin() and end() are the same, so a loop does not run.

C++
#include <iostream>
#include <set>

int main() {
    std::set<int> s;
    std::cout << std::boolalpha;

    std::cout << "begin == end: " << (s.begin() == s.end()) << '\n';

    int loops = 0;
    for (auto it = s.begin(); it != s.end(); ++it) {
        ++loops;
    }
    std::cout << "loop ran " << loops << " times\n";

    s.insert(1);
    std::cout << "begin == end now: " << (s.begin() == s.end()) << '\n';
    return 0;
}

How It Works

  1. An empty set has no first value, so begin() and end() are the same position, and the comparison is true.
  2. The loop condition it != s.end() is false at once, so the body never runs and loops stays 0.
  3. After insert(1), there is a real first value. begin() now points at it, and end() is still one position after, so they differ.

Frequently Asked Questions

An iterator to the position after the last value. It does not point at a real value.
No. That is undefined behavior. Only compare it with other iterators.
Use std::prev(s.end()) or s.rbegin(), after checking that the set is not empty.
A search needs a way to say "not found". Because end() is never a real value, it is a safe signal.
It equals begin(), so any loop from begin() to end() does nothing.

Next: equal_range()

Keep going through the std::set functions. Get the range of values equal to the given value.

equal_range() 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