C++ std::unordered_set end() Function

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

Definition

In C++, std::unordered_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() or equal_range(), which return end() when nothing matches. 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 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
}
for (auto p = s.begin(); p != s.end(); ++p) {
    // ...
}
CallWhat it does
s.end()Returns an iterator one past the last value. On a const unordered_set it returns a const_iterator.

Do not write *s.end() and do not use s.end() - 1. Unordered_set iterators are forward iterators: there is no reverse walk and no random access. Prefer find() over guessing which value is “last”.

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 unordered_set is empty.

C++ Examples

Example 1 — end() Means Not Found

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

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

int main() {
    std::unordered_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() on an Empty Set

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

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

int main() {
    std::unordered_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 unordered_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.

Example 3 — end() Stops a Membership Check

Walk until end() while looking for a target without using find().

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

int main() {
    std::unordered_set<std::string> tags = {"cpp", "hash", "set"};
    const std::string want = "hash";

    bool found = false;
    for (auto it = tags.begin(); it != tags.end(); ++it) {
        if (*it == want) {
            found = true;
            break;
        }
    }

    std::cout << std::boolalpha;
    std::cout << "found hash by walk: " << found << '\n';
    std::cout << "find agrees: " << (tags.find(want) != tags.end()) << '\n';
    return 0;
}

How It Works

  1. The loop starts at begin() and stops at end() or when the target matches.
  2. "hash" is stored, so found becomes true and break ends the walk early.
  3. find() reports the same answer in average constant time. Prefer find() in real code; the loop only shows what end() means.

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.
A search needs a way to say "not found". Because end() is never a real value, it is a safe signal.
No. There are no reverse iterators. Values are not sorted, so there is no "last" value in sorted order.
It equals begin(), so any loop from begin() to end() does nothing.

Next: equal_range()

Keep going through the std::unordered_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.

10 people found this page helpful