In C++, std::map::clear()removes every pair from a std::map. After the call, size() is 0 and empty() is true.
The map object itself stays alive and you can insert new pairs into it right away. 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 clear with empty brackets. It takes no argument and returns nothing.
C++
m.clear(); // map is now empty
std::cout << m.size(); // 0
Call
What it does
m.clear()
Destroys all pairs. The map becomes empty and can be used again.
clear() removes all pairs. To remove one key, use erase(key). To only ask whether the map is empty, use empty().
Library declarations
C++
void clear() noexcept;
Parameters
Parameter
Description
None
clear() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
void
Nothing is returned. The map is empty afterwards.
C++ Examples
Example 1 — clear() Removes All Pairs
Fill a map, print its size, clear it, and print the size again.