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