C++ std::set empty() Function

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

Definition

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

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

C++
std::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 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 set has no values.
falseThe 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 <set>

int main() {
    std::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. A 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 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 the smallest score only when a score exists.

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

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

int main() {
    std::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 set the function prints a message and returns, so it never reads *begin().
  2. The first call uses an empty set, so you see no scores yet. Reading *scores.begin() here would be undefined behavior.
  3. After inserting 40 and 15, the 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 a set one by one until nothing is left.

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

int main() {
    std::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 set keeps its text values in alphabetical 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 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 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 a 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 set is undefined behavior, so check empty() first.
No. The first insert stores the value, so the set is not empty. A repeated insert of the same value does not add another copy.

Next: end()

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

8 people found this page helpful