In C++, std::unordered_map::clear()removes every pair from a std::unordered_map. After the call, size() is 0 and empty() is true.
The unordered_map object itself stays alive and you can insert new pairs into it right away. 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 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 unordered_map becomes empty and can be used again.
clear() removes all pairs. To remove one key, use erase(key). To only ask whether the unordered_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 unordered_map is empty afterwards.
C++ Examples
Example 1 — clear() Removes All Pairs
Fill an unordered_map, print its size, clear it, and print the size again.