C++ std::unordered_set empty() Function

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

Definition

In C++, std::unordered_set::empty() tells you whether an unordered_set has no values. It returns true for an empty unordered_set and false when there is at least one value.

It does not change the unordered_set, and it takes constant time. Use it before you read *s.begin(), because that is not safe on an empty unordered_set. 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 it with empty brackets. No value goes inside.

C++
std::unordered_set<int> s;
bool none = s.empty();     // true
s.insert(3);
if (!s.empty()) {
    // at least one value
}
CallWhat it does
s.empty()Returns true if the unordered_set has no values, otherwise false.

empty() is the same as asking size() == 0, but it says what you mean. Do not mix it up with clear(). The function empty() only asks, while clear() removes every value.

Library declarations
C++
[[nodiscard]] bool empty() const noexcept;   // [[nodiscard]] since C++20

Parameters

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

Return Value

Return ValueDescription
trueThe unordered_set has no values.
falseThe unordered_set has at least one value.

C++ Examples

Example 1 — empty() Before and After insert()

Ask a new set if it is empty, add one value, and ask again.

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

int main() {
    std::unordered_set<int> s;

    std::cout << std::boolalpha;
    std::cout << "empty at start: " << s.empty() << '\n';

    s.insert(7);
    std::cout << "empty after insert: " << s.empty() << '\n';
    std::cout << "size: " << s.size() << '\n';
    return 0;
}

How It Works

  1. An unordered_set created with no values is empty, so the first empty() prints true. std::boolalpha makes the output say true instead of 1.
  2. insert(7) adds one value. Now the unordered_set is not empty, so the second call prints false.
  3. The size is 1. This matches the idea that empty() is false exactly when size() is not zero.

Example 2 — empty() Protects begin()

Read some score only when a score exists.

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

void showLowest(const std::unordered_set<int>& scores) {
    if (scores.empty()) {
        std::cout << "no scores yet\n";
        return;
    }
    std::cout << "lowest: " << *scores.begin() << '\n';
}

int main() {
    std::unordered_set<int> scores;
    showLowest(scores);

    scores.insert(40);
    scores.insert(15);
    showLowest(scores);
    return 0;
}

How It Works

  1. showLowest() first checks scores.empty(). On an empty unordered_set the function prints a message and returns, so it never reads *begin().
  2. The first call uses an empty unordered_set, so you see no scores yet. Reading *scores.begin() here would be undefined behavior.
  3. After inserting 40 and 15, the unordered_set is sorted as 15, 40. The smallest value is first, so the second call prints lowest: 15.

Example 3 — empty() as a Loop Condition

Take tasks out of an unordered_set one by one until nothing is left.

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

int main() {
    std::unordered_set<std::string> tasks = {"write", "test", "deploy"};

    while (!tasks.empty()) {
        std::string next = *tasks.begin();
        std::cout << "doing: " << next << '\n';
        tasks.erase(tasks.begin());
    }

    std::cout << "all done, empty: " << std::boolalpha << tasks.empty() << '\n';
    return 0;
}

How It Works

  1. The unordered_set keeps its text values in an unspecified order, so the first task is always the alphabetically smallest one.
  2. Each loop turn copies *tasks.begin(), prints it, and then erases that position. The unordered_set gets one value smaller every turn, so the order is deploy, test, write.
  3. When the last value is erased, tasks.empty() becomes true. The condition !tasks.empty() turns false and the loop stops.

Frequently Asked Questions

It returns true if the unordered_set has no values and false if it has at least one value.
No. empty() only checks. To remove all values, call clear().
Use empty(). It does the same job and shows your intention. For an unordered_set both are fast, but empty() is the clearer choice.
Calling begin() is safe, but it equals end(). Dereferencing it with *s.begin() on an empty unordered_set is undefined behavior, so check empty() first.
No. The first insert stores the value, so the unordered_set is not empty. A repeated insert of the same value does not add another copy.

Next: end()

Keep going through the std::unordered_set functions. Get the iterator just past the last value.

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