C++ std::set erase() Function

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

Definition

In C++, std::set::erase() removes values from the 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 set. It just returns 0. The set stays sorted after every erase. 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

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.begin());           // by iterator
s.erase(s.lower_bound(40), s.end());      // a range
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. A 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 a 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 <set>

int main() {
    std::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 << "left:";
    for (int x : s) {
        std::cout << ' ' << x;
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. erase(3) finds 3 in the 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 set, so it returns 0.
  3. The remaining values print in sorted order: 1 2 4. Because a set holds a value only once, the count can never be bigger than 1.

Example 2 — erase() with Iterators

Remove one element by iterator, then cut off the tail with a range.

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

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

    auto it = s.find(20);
    it = s.erase(it);
    std::cout << "after erasing 20, next is " << *it << '\n';

    s.erase(s.lower_bound(40), s.end());

    std::cout << "left:";
    for (int x : s) {
        std::cout << ' ' << x;
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. find(20) gives an iterator to 20. erase(it) removes it and returns an iterator to the element after it, which is 30.
  2. lower_bound(40) points to 40 and end() is the end. The range form removes 40 and 50 together.
  3. Only 10 and 30 remain. Note that it was reassigned from the return value, so it never points to a removed element.

Example 3 — erase() While Looping

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

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

int main() {
    std::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 << "odd numbers:";
    for (int x : s) {
        std::cout << ' ' << x;
    }
    std::cout << '\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. The set ends with 1 3 5 7.

Frequently Asked Questions

The value form returns how many values were removed, which is 0 or 1 for a set. The iterator forms return an iterator to the element after the removed one.
Nothing happens. erase() returns 0 and the 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 set.

Next: extract()

Keep going through the std::set functions. Take a value out of the 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.

11 people found this page helpful