Home C++ Maps Functions empty() C++ std::map empty() Function 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.
bool none = m.empty(); // true if the map has no pairs
if (!m.empty()) {
// safe to use m.begin()
} Call What 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 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 map has no elements. falseThe 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 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 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 <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;
} 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 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 <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;
} no scores yet
2 score(s) saved How It Works The function takes the map by const reference, so there is no copy and empty() can still be called on a const map. The first call passes a 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 Take pairs off the front of a map one by one until nothing is left.
#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;
} 1 one
2 two
3 three
map is empty now How It Works The map keeps its keys sorted, so even though 3 was written first, begin() points at the smallest key, 1. Each turn prints the first pair and erases it. The loop runs while !todo.empty() is true, so it runs exactly three times. When the last pair is erased, empty() becomes true, the loop ends, and the final message is printed. Frequently Asked Questions What does std::map::empty() return? 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.
What is the difference between empty() and clear()? empty() only asks whether the map is empty. clear() actually removes every pair. Calling empty() never changes the map.
Should I use empty() or size() == 0? Use empty(). For std::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::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.
9 people found this page helpful
Helpful Share Copy link Suggestion