C++ std::map empty() Function

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

Definition

In C++, std::map::empty() tells you whether a std::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 a map, use clear(). Include <map> to use it.

This is a member of std::map<Key, T>, a sorted container of key and value pairs. Each key is unique and the pairs are always kept in key order, so a map can find, insert, and erase a key in logarithmic time. 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 map has no pairs
if (!m.empty()) {
    // safe to use m.begin()
}
CallWhat it does
m.empty()Returns true if the 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 map has no elements.
falseThe 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 <map>
#include <string>

int main() {
    std::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 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 <map>
#include <string>

void report(const std::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::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 map by const reference, so there is no copy and empty() can still be called on a const map.
  2. The first call passes a 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

Take pairs off the front of a map one by one until nothing is left.

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

int main() {
    std::map<int, std::string> todo = {{3, "three"}, {1, "one"}, {2, "two"}};

    while (!todo.empty()) {
        auto it = todo.begin();
        std::cout << it->first << " " << it->second << '\n';
        todo.erase(it);
    }

    std::cout << "map is empty now\n";
    return 0;
}

How It Works

  1. The map keeps its keys sorted, so even though 3 was written first, begin() points at the smallest key, 1.
  2. Each turn prints the first pair and erases it. The loop runs while !todo.empty() is true, so it runs exactly three times.
  3. When the last pair is erased, empty() becomes true, the loop ends, and the final message is printed.

Frequently Asked Questions

It returns true if the map has no elements and false if it has at least one. It takes no arguments and does not change the map.
empty() only asks whether the map is empty. clear() actually removes every pair. Calling empty() never changes the map.
Use empty(). For std::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::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.

9 people found this page helpful