C++ std::unordered_map empty() Function

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

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.

C++
bool none = m.empty();      // true if the unordered_map has no pairs
if (!m.empty()) {
    // safe to use m.begin()
}
CallWhat 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
C++
bool empty() const noexcept;
// C++20: [[nodiscard]] bool empty() const noexcept;

Parameters

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

Return Value

Return ValueDescription
trueThe unordered_map has no elements.
falseThe unordered_map has one or more elements.

C++ Examples

Example 1 — empty() on a New Map

Print the result of empty() before and after adding and removing a pair.

C++
#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;
}

How It Works

  1. std::boolalpha makes cout print true and false instead of 1 and 0.
  2. A new map has no pairs, so empty() is true. After insert({1, "one"}) it holds one pair, so the answer is false.
  3. 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.

C++
#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;
}

How It Works

  1. The function takes the unordered_map by const reference, so there is no copy and empty() can still be called on a const map.
  2. The first call passes an unordered_map with no pairs, so empty() is true and the message is printed.
  3. 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.

C++
#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;
}

How It Works

  1. The unordered_map starts with three pairs. The loop condition is !todo.empty().
  2. Each turn erases whatever pair begin() currently points at. Which key is first is unspecified, so the demo counts removals instead of printing keys.
  3. After three erases, empty() is true and the loop stops.

Frequently Asked Questions

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.
empty() only asks whether the unordered_map is empty. clear() actually removes every pair. Calling empty() never changes the unordered_map.
Use empty(). For std::unordered_map both are fast, but empty() is clearer and it works the same way on every container.
Yes, but it returns end(), so you must not dereference it. Check !m.empty() before you read m.begin()->first.
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

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.

10 people found this page helpful