C++ std::set clear() Function

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

Definition

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

The 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 <set> to use it.

This is a member of std::set<T>, a sorted container of unique values. A set never keeps a duplicate and always holds its values in sorted order, so it can find, insert, and erase a value in logarithmic time. A set has no operator[] and no at(), because the value is its own key. Every element is const, so you cannot change a value through an iterator.

Syntax

Call it with empty brackets. It removes everything.

C++
std::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(). A 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 set is empty after the call.

C++ Examples

Example 1 — clear() Removes Everything

Fill a set with three numbers, clear it, and check the size.

C++
#include <iostream>
#include <set>

int main() {
    std::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 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 set is a normal empty 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 <set>
#include <vector>

int main() {
    std::vector<std::vector<int>> batches = {{1, 1, 2}, {5, 6, 7, 5}};
    std::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 set drops the repeat, so it holds 1 and 2 and reports 2 unique values.
  3. seen.clear() empties the 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 a set, clear the original, and see that the copy keeps its values.

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

int main() {
    std::set<std::string> original = {"red", "green", "blue"};
    std::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 sorted order: blue, green, red. This is a simple way to keep a snapshot before you clear a set.

Frequently Asked Questions

It removes all values from the set. The 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 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. A set does not keep a spare buffer the way a vector can.

Next: contains()

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

10 people found this page helpful