C++ std::unordered_map clear() Function

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

Definition

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
CallWhat 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

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

Return Value

Return ValueDescription
voidNothing 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.

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

int main() {
    std::unordered_map<std::string, int> m = {{"red", 1}, {"green", 2}, {"blue", 3}};
    std::cout << "before: " << m.size() << '\n';

    m.clear();
    std::cout << "after: " << m.size() << '\n';
    std::cout << "empty: " << std::boolalpha << m.empty() << '\n';
    return 0;
}

How It Works

  1. The unordered_map starts with three colour pairs, so the first line shows 3.
  2. m.clear() erases all three pairs in one call. No loop is needed.
  3. The size is 0, and empty() reports true, so the unordered_map really is empty.

Example 2 — Reuse a Map After clear()

Clear an unordered_map and insert fresh data. The old keys are gone.

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

int main() {
    std::unordered_map<int, std::string> m = {{1, "old"}, {2, "older"}};

    m.clear();
    m[10] = "new";

    std::cout << "count(1): " << m.count(1) << '\n';
    std::cout << "count(10): " << m.count(10) << '\n';
    std::cout << "size: " << m.size() << '\n';
    return 0;
}

How It Works

  1. The unordered_map first holds keys 1 and 2. clear() deletes both pairs.
  2. m[10] = "new" adds a fresh pair. The unordered_map object works as usual after a clear.
  3. count(1) is 0 because the old key is gone. count(10) is 1 and the size is 1.

Example 3 — clear() in a Loop

Reuse one map for several rounds of work, clearing it at the end of each round.

C++
#include <iostream>
#include <unordered_map>

int main() {
    std::unordered_map<int, int> squares;

    for (int round = 1; round <= 2; ++round) {
        for (int i = 1; i <= round + 1; ++i) {
            squares[i] = i * i * round;
        }
        std::cout << "round " << round << ": " << squares.size()
                  << " pairs, key2=" << squares.at(2) << '\n';
        squares.clear();
    }

    std::cout << "left over: " << squares.size() << '\n';
    return 0;
}

How It Works

  1. In round 1 the keys 1 and 2 are stored. at(2) reads value 4 without relying on iteration order.
  2. clear() empties the unordered_map. Round 2 starts fresh with keys 1 to 3, and at(2) is 2 * 2 * 2 = 8.
  3. The unordered_map is cleared again at the end of round 2, so nothing is left over and the final size is 0.

Frequently Asked Questions

It removes all key and value pairs from the unordered_map. Afterwards size() is 0 and empty() is true.
clear() removes everything. erase() removes one key, one iterator, or a range of iterators.
It destroys every pair and frees every node. The unordered_map object itself stays, and you can insert into it again.
No. If the values are raw pointers from new, you must call delete on them yourself before clear(), or use std::unique_ptr so the cleanup is automatic.
No. Every iterator, pointer, and reference to the old pairs becomes invalid. The end() iterator stays valid.

Next: contains()

Keep going through the std::unordered_map functions. Return true if the key is in the unordered_map (C++20).

contains() 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.

14 people found this page helpful