C++ std::unordered_set erase() Function

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

Definition

In C++, std::unordered_set::erase() removes values from the unordered_set. You can give it a value, an iterator to one element, or a pair of iterators for a range.

Erasing by value is safe even when the value is not in the unordered_set. It just returns 0. Values stay unique and hash-bucket order can change after removals. 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

Pass a value, an iterator, or a range.

C++
std::size_t n = s.erase(30);              // by value: 1 or 0
auto next = s.erase(s.find(20));          // by iterator
s.erase(s.find(40));                      // remove one value
CallWhat it does
s.erase(value)Removes the value if it exists. Returns 1 if it was removed and 0 if it was not found.
s.erase(pos)Removes the element that pos points to. Returns an iterator to the next element.
s.erase(first, last)Removes every element from first up to, but not including, last. Returns last.

Never call erase(pos) with end(), because it points to no element. To erase while you loop, always use the iterator that erase() returns. An unordered_set does not move other values when one is removed, so all other iterators stay valid.

Library declarations
C++
iterator erase(iterator pos);
iterator erase(const_iterator pos);
iterator erase(const_iterator first, const_iterator last);
size_type erase(const key_type& key);

Parameters

ParameterDescription
valueThe value to remove.
posA valid iterator to one element. It must not be end().
first, lastThe range to remove. last is one past the final element to remove.

Return Value

Return ValueDescription
size_typeFor the value form: how many were removed. For an unordered_set this is 0 or 1.
iteratorFor the iterator forms: the element after the removed one, or end().

C++ Examples

Example 1 — erase() by Value

Remove one stored value and one value that is missing, and read the counts.

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

int main() {
    std::unordered_set<int> s = {1, 2, 3, 4};

    std::size_t a = s.erase(3);
    std::size_t b = s.erase(9);
    std::cout << "erase 3: " << a << '\n';
    std::cout << "erase 9: " << b << '\n';
    std::cout << "size: " << s.size() << '\n';
    std::cout << std::boolalpha;
    std::cout << "has 1: " << (s.count(1) == 1) << '\n';
    std::cout << "has 3: " << (s.count(3) == 1) << '\n';
    return 0;
}

How It Works

  1. erase(3) finds 3 in the unordered_set and removes it, so it returns 1.
  2. erase(9) finds nothing to remove. It does not throw and it does not change the unordered_set, so it returns 0.
  3. The size is 3. Membership checks confirm 1 is still there and 3 is gone, without relying on iteration order.

Example 2 — erase() with an Iterator

Remove one element by iterator and check what remains.

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

int main() {
    std::unordered_set<int> s = {10, 20, 30, 40, 50};

    auto it = s.find(20);
    if (it != s.end()) {
        s.erase(it);
    }
    s.erase(40);
    s.erase(50);

    std::cout << "size: " << s.size() << '\n';
    std::cout << std::boolalpha;
    std::cout << "has 20: " << (s.find(20) != s.end()) << '\n';
    std::cout << "has 10: " << (s.find(10) != s.end()) << '\n';
    std::cout << "has 30: " << (s.find(30) != s.end()) << '\n';
    return 0;
}

How It Works

  1. find(20) gives an iterator to 20. erase(it) removes that element.
  2. erase(40) and erase(50) remove those values by value. There is no sorted range erase like lower_bound on an unordered_set.
  3. Three values remain. Membership checks prove 20 is gone while 10 and 30 stay.

Example 3 — erase() While Looping

Remove all even numbers by using the iterator that erase() returns.

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

int main() {
    std::unordered_set<int> s = {1, 2, 3, 4, 5, 6, 7, 8};

    for (auto it = s.begin(); it != s.end();) {
        if (*it % 2 == 0) {
            it = s.erase(it);
        } else {
            ++it;
        }
    }

    std::cout << "size: " << s.size() << '\n';
    std::cout << std::boolalpha;
    std::cout << "has 1: " << (s.count(1) == 1) << '\n';
    std::cout << "has 2: " << (s.count(2) == 1) << '\n';
    std::cout << "has 7: " << (s.count(7) == 1) << '\n';
    return 0;
}

How It Works

  1. The for loop has no ++it in its header. The loop moves forward by itself inside the body.
  2. When the value is even, it = s.erase(it) removes it and moves it to the next element. When the value is odd, ++it moves on without removing anything.
  3. Each step moves forward exactly once, so no element is skipped and no removed iterator is used. Four odd values remain; membership checks prove 2 is gone.

Frequently Asked Questions

The value form returns how many values were removed, which is 0 or 1 for an unordered_set. The iterator forms return an iterator to the element after the removed one.
Nothing happens. erase() returns 0 and the unordered_set is unchanged. It does not throw.
Use the returned iterator: it = s.erase(it). Do not use the old iterator after erasing, because that element is gone.
Yes, but check that the iterator is not end() first. Erasing end() is undefined behavior. Erasing by value is simpler when you do not need the iterator.
erase() removes chosen values. clear() removes all of them and leaves an empty unordered_set.

Next: extract()

Keep going through the std::unordered_set functions. Take a value out of the unordered_set as a node you can edit or move (C++17).

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

12 people found this page helpful