Home C++ Unordered Maps Functions empty() C++ std::unordered_map empty() Function Definition In C++, std::unordered_map::empty() tells you whether a std::unordered_map has no elements at all . It returns true for an empty map and false otherwise.
Use it before you read the first or last element, or to decide whether to print a message. It does not remove anything. To empty an unordered_map, use clear(). Include <unordered_map> to use it.
This is a member of std::unordered_map<Key, T>, a hash table of key and value pairs . Each key is unique. Find, insert, and erase are usually average constant time. Unlike std::map, keys are not sorted and iteration order is unspecified. Read a value with at() or find(). The m[key] operator also works, but it adds a default value when the key is missing.
Syntax Call empty with empty brackets. It takes no argument.
bool none = m.empty(); // true if the unordered_map has no pairs
if (!m.empty()) {
// safe to use m.begin()
} Call What it does m.empty()Returns true if the unordered_map has no elements, and false if it has at least one.
m.empty() is the same as m.size() == 0 but it says what you mean. Do not confuse it with clear(): empty() only asks a question.
Library declarations bool empty() const noexcept;
// C++20: [[nodiscard]] bool empty() const noexcept; Parameters Parameter Description None empty() takes no parameters. Call it with empty brackets.
Return Value Return Value Description trueThe unordered_map has no elements. falseThe unordered_map has one or more elements.
Important
Since C++20 the result is marked [[nodiscard]], so a line like m.empty(); that ignores the answer gives a warning. Many beginners type it hoping it will clear the unordered_map. To remove all pairs, call m.clear().
C++ Examples 1. Check an empty map 2. Use it in a function 3. Loop until the unordered_map is empty Example 1 — empty() on a New Map Print the result of empty() before and after adding and removing a pair.
#include <iostream>
#include <unordered_map>
#include <string>
int main() {
std::unordered_map<int, std::string> m;
std::cout << std::boolalpha;
std::cout << "new map: " << m.empty() << '\n';
m.insert({1, "one"});
std::cout << "after insert: " << m.empty() << '\n';
m.erase(1);
std::cout << "after erase: " << m.empty() << '\n';
return 0;
} new map: true
after insert: false
after erase: true How It Works std::boolalpha makes cout print true and false instead of 1 and 0.A new map has no pairs, so empty() is true. After insert({1, "one"}) it holds one pair, so the answer is false. erase(1) removes the only pair, which makes the unordered_map empty again, so the last line shows true.Example 2 — empty() in a Helper Function Write a function that prints a friendly message for an empty map and a count for any other map.
#include <iostream>
#include <unordered_map>
#include <string>
void report(const std::unordered_map<std::string, int>& scores) {
if (scores.empty()) {
std::cout << "no scores yet\n";
} else {
std::cout << scores.size() << " score(s) saved\n";
}
}
int main() {
std::unordered_map<std::string, int> scores;
report(scores);
scores["Ann"] = 90;
scores["Ben"] = 75;
report(scores);
return 0;
} no scores yet
2 score(s) saved How It Works The function takes the unordered_map by const reference, so there is no copy and empty() can still be called on a const map. The first call passes an unordered_map with no pairs, so empty() is true and the message is printed. Two pairs are added with []. The second call finds empty() false and prints size(), which is 2. Example 3 — empty() as a Loop Condition Erase pairs one by one until nothing is left, counting how many were removed.
#include <iostream>
#include <unordered_map>
#include <string>
int main() {
std::unordered_map<int, std::string> todo = {{3, "three"}, {1, "one"}, {2, "two"}};
int removed = 0;
while (!todo.empty()) {
todo.erase(todo.begin());
++removed;
}
std::cout << "removed: " << removed << '\n';
std::cout << "empty now: " << std::boolalpha << todo.empty() << '\n';
return 0;
} removed: 3
empty now: true How It Works The unordered_map starts with three pairs. The loop condition is !todo.empty(). Each turn erases whatever pair begin() currently points at. Which key is first is unspecified, so the demo counts removals instead of printing keys. After three erases, empty() is true and the loop stops. Frequently Asked Questions What does std::unordered_map::empty() return? It returns true if the unordered_map has no elements and false if it has at least one. It takes no arguments and does not change the unordered_map.
What is the difference between empty() and clear()? empty() only asks whether the unordered_map is empty. clear() actually removes every pair. Calling empty() never changes the unordered_map.
Should I use empty() or size() == 0? Use empty(). For std::unordered_map both are fast, but empty() is clearer and it works the same way on every container.
Is it safe to call begin() on an empty map? Yes, but it returns end(), so you must not dereference it. Check !m.empty() before you read m.begin()->first.
Why does the compiler warn about a call to empty()? In C++20 empty() is marked [[nodiscard]]. The warning tells you that you ignored the answer. If you wanted to remove everything, use clear().
Next: end() Keep going through the std::unordered_map functions. Get the iterator just past the last pair.
end() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
10 people found this page helpful
Helpful Share Copy link Suggestion