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
}
Call
What 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
Parameter
Description
None
empty() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
true
The set has no values.
false
The 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.
showLowest() first checks scores.empty(). On an empty set the function prints a message and returns, so it never reads *begin().
The first call uses an empty set, so you see no scores yet. Reading *scores.begin() here would be undefined behavior.
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;
}
Output
doing: deploy
doing: test
doing: write
all done, empty: true
How It Works
The set keeps its text values in alphabetical order, so the first task is always the alphabetically smallest one.
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.
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.