C++ std::unordered_set clear() Function

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

Definition

In C++, std::unordered_set::clear() removes all values from the unordered_set. After the call, size() is 0 and empty() is true.

The unordered_set object itself stays alive and works as before, so you can insert new values at once. Every iterator and reference to the old values becomes invalid. Include <unordered_set> to use it.

This is a member of std::unordered_set<T>, a hash table of unique values. Find, insert, and erase are usually average constant time. Unlike std::set, values are not sorted and iteration order is unspecified. Elements are const, so you cannot change a stored value through an iterator. There is no operator[] and no at(). Prefer find() or contains() to look up a value, and call reserve() before many inserts.

Syntax

Call it with empty brackets. It removes everything.

C++
std::unordered_set<int> s = {1, 2, 3};
s.clear();
// s.size() is 0
// s.empty() is true
CallWhat it does
s.clear()Removes all values. The call returns nothing.

clear() works in linear time, because each value must be destroyed. To remove only some values, use erase(). An unordered_set has no capacity(), so there is no extra reserved memory to worry about after clear().

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_set is empty after the call.

C++ Examples

Example 1 — clear() Removes Everything

Fill an unordered_set with three numbers, clear it, and check the size.

C++
#include <iostream>
#include <unordered_set>

int main() {
    std::unordered_set<int> s = {1, 2, 3};
    std::cout << "before: " << s.size() << '\n';

    s.clear();

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

How It Works

  1. The unordered_set starts with three values, so the first line prints 3.
  2. s.clear() destroys every value in one call. There is no loop and no argument.
  3. The size is now 0 and empty() returns true. The unordered_set is a normal empty unordered_set, ready for new values.

Example 2 — clear() to Reuse a Set

Count the different values in each batch with a single set that is cleared between batches.

C++
#include <iostream>
#include <unordered_set>
#include <vector>

int main() {
    std::vector<std::vector<int>> batches = {{1, 1, 2}, {5, 6, 7, 5}};
    std::unordered_set<int> seen;

    for (std::size_t i = 0; i < batches.size(); ++i) {
        for (int v : batches[i]) {
            seen.insert(v);
        }
        std::cout << "batch " << i + 1 << ": " << seen.size() << " unique\n";
        seen.clear();
    }
    return 0;
}

How It Works

  1. There are two batches of numbers. One set called seen is used for both of them.
  2. Batch 1 is 1, 1, 2. The unordered_set drops the repeat, so it holds 1 and 2 and reports 2 unique values.
  3. seen.clear() empties the unordered_set before batch 2. If it was skipped, the old values would stay and the second count would be wrong. Batch 2 is 5, 6, 7, 5, which gives 3 unique values.

Example 3 — clear() Only Affects One Set

Copy an unordered_set, clear the original, and see that the copy keeps its values.

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

int main() {
    std::unordered_set<std::string> original = {"red", "green", "blue"};
    std::unordered_set<std::string> backup = original;

    original.clear();

    std::cout << "original: " << original.size() << '\n';
    std::cout << "backup: " << backup.size() << '\n';
    for (const std::string& c : backup) {
        std::cout << c << '\n';
    }
    return 0;
}

How It Works

  1. backup = original makes a full, separate copy of the three colors. The two sets share nothing.
  2. original.clear() removes the values from original only, so its size is 0 and the backup size is still 3.
  3. The loop prints the backup in an unspecified order: blue, green, red. This is a simple way to keep a snapshot before you clear an unordered_set.

Frequently Asked Questions

It removes all values from the unordered_set. The unordered_set is then empty, with a size of 0.
clear() always removes everything. erase() removes one value, one position, or one range, so it can remove only some values.
Yes. The unordered_set is still valid and empty. You can insert new values right away.
No. All iterators, pointers, and references to the old values become invalid. Get new ones after the call.
Yes. Each value lives in its own node, and clear() destroys every node. An unordered_set does not keep a spare buffer the way a vector can.

Next: contains()

Keep going through the std::unordered_set functions. Return true if the value is in the unordered_set (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.

13 people found this page helpful