C++ std::unordered_set begin() Function

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

Definition

In C++, std::unordered_set::begin() returns an iterator to one value in a std::unordered_set. The first position is unspecified, because values live in hash buckets and are not sorted.

Through the iterator, *it reads the value. Together with end() it lets you walk every value. Do not rely on the visit order. 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 begin with empty brackets. Compare with end() to know when to stop.

C++
auto it = s.begin();
for (auto p = s.begin(); p != s.end(); ++p) {
    // *p  (order is unspecified)
}
CallWhat it does
s.begin()Returns an iterator to one value. On a const unordered_set it returns a const_iterator.

If the unordered_set is empty, begin() equals end(). A range-based for (const auto& v : s) uses begin() and end() for you. Unlike std::set, there is no guaranteed sorted walk.

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

Parameters

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

Return Value

Return ValueDescription
iteratorAn iterator to one value. For an empty unordered_set it equals end(), and it must not be dereferenced.

C++ Examples

Example 1 — begin() Needs a Non-Empty Check

Insert values and read through begin() only when the unordered_set is not empty.

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

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

    s.insert(30);
    s.insert(10);
    s.insert(20);

    if (!s.empty()) {
        std::cout << "has value via begin: " << (*s.begin() == 10 || *s.begin() == 20 || *s.begin() == 30) << '\n';
    }
    std::cout << "size: " << s.size() << '\n';
    return 0;
}

How It Works

  1. On an empty unordered_set begin() equals end(), so the first line prints true.
  2. After three inserts the table holds 10, 20, and 30. Which one begin() points at is unspecified.
  3. The check proves that *begin() is one of those three values, and size() is 3. The demo never prints a single ordered list.

Example 2 — begin() in a Loop

Walk every value and count how many are greater than 5.

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

int main() {
    std::unordered_set<int> nums = {2, 9, 4, 11};

    int big = 0;
    for (auto it = nums.begin(); it != nums.end(); ++it) {
        if (*it > 5) {
            ++big;
        }
    }

    std::cout << "values > 5: " << big << '\n';
    std::cout << "size: " << nums.size() << '\n';
    return 0;
}

How It Works

  1. begin() starts the walk at an unspecified value. The loop continues until end().
  2. Each step reads *it. Values 9 and 11 raise the counter; 2 and 4 do not.
  3. The printed answers are counts, so they do not depend on hash-bucket order.

Example 3 — begin() Versus find()

Use find() when you care about a specific value; use begin() only to start a full walk.

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

int main() {
    std::unordered_set<std::string> fruit = {"pear", "apple", "kiwi"};

    auto apple = fruit.find("apple");
    auto missing = fruit.find("mango");

    std::cout << std::boolalpha;
    std::cout << "apple found: " << (apple != fruit.end()) << '\n';
    std::cout << "mango found: " << (missing != fruit.end()) << '\n';
    std::cout << "begin != end: " << (fruit.begin() != fruit.end()) << '\n';
    std::cout << "size: " << fruit.size() << '\n';
    return 0;
}

How It Works

  1. find("apple") returns an iterator to "apple" because that value is stored.
  2. find("mango") returns end(). That is the safe way to ask for one specific value.
  3. begin() != end() only says the unordered_set is not empty. size() is 3. Prefer find() when you care about a named value.

Frequently Asked Questions

An iterator to one value in the unordered_set. Which value is first is unspecified because of hashing.
It equals end(). You can compare it, but you must not dereference it.
No. Elements in an unordered_set are const. Erase the old value and insert the new one.
In an unspecified hash-bucket order. Unlike std::set, the walk is not sorted.
On a non-const unordered_set begin() returns iterator and cbegin() returns const_iterator. Elements stay read-only either way.

Next: bucket()

Keep going through the std::unordered_set functions. Which bucket a value would use (or already uses).

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

7 people found this page helpful