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
Call
What 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.
A valid iterator to one element. It must not be end().
first, last
The range to remove. last is one past the final element to remove.
Return Value
Return Value
Description
size_type
For the value form: how many were removed. For a set this is 0 or 1.
iterator
For 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;
}
Output
erase 3: 1
erase 9: 0
left: 1 2 4
How It Works
erase(3) finds 3 in the set and removes it, so it returns 1.
erase(9) finds nothing to remove. It does not throw and it does not change the set, so it returns 0.
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;
}
Output
after erasing 20, next is 30
left: 10 30
How It Works
find(20) gives an iterator to 20. erase(it) removes it and returns an iterator to the element after it, which is 30.
lower_bound(40) points to 40 and end() is the end. The range form removes 40 and 50 together.
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;
}
Output
odd numbers: 1 3 5 7
How It Works
The for loop has no ++it in its header. The loop moves forward by itself inside the body.
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.
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.