In C++, std::list::erase() removes elements from a std::list using iterators. You can erase one element, or a whole range from first up to (but not including) last.
The nodes are unlinked and destroyed, and nothing else moves. Include <list> to use it.
This is a member of std::list<T>, a doubly linked list. Every element lives in its own node with a link to the previous and the next node, so a list can insert and erase anywhere quickly once you hold an iterator. It has no access by index, so l[i] does not exist.
Syntax
Call erase with an iterator, or with two iterators for a range.
C++
auto next = l.erase(pos); // erase one element
auto after = l.erase(first, last); // erase [first, last)
Call
What it does
l.erase(pos)
Erases the element that pos points to. Returns an iterator to the element after it.
l.erase(first, last)
Erases every element in [first, last). Returns last (now valid again).
To erase by value instead of by position, use l.remove(value). To erase by a rule, use l.remove_if(pred).
The iterator of the single element to erase. It must be valid and not end().
first, last
The range to erase. first is included, last is not. last may be end().
Return Value
Return Value
Description
iterator
An iterator to the element that came right after the last erased element. If the erased element was the last one, it is end().
C++ Examples
Example 1 — erase() One Element
Erase the second element and use the iterator that comes back.
C++
#include <iostream>
#include <iterator>
#include <list>
int main() {
std::list<int> l = {10, 20, 30, 40};
auto it = std::next(l.begin());
it = l.erase(it);
std::cout << "next: " << *it << '\n';
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
next: 30
10 30 40
How It Works
std::next(l.begin()) gives an iterator to the second node, which holds 20.
erase(it) unlinks that node. The old it is now invalid, so we replace it with the returned iterator.
The returned iterator points at 30, the element after the erased one. The list is now 10 30 40.
Example 2 — erase() a Range of Elements
Remove the middle of a list with two iterators. The second iterator is one place after the last element to erase.
C++
#include <iostream>
#include <iterator>
#include <list>
int main() {
std::list<int> l = {1, 2, 3, 4, 5, 6};
l.erase(std::next(l.begin(), 1), std::next(l.begin(), 4));
for (int n : l) {
std::cout << n << ' ';
}
std::cout << "(size " << l.size() << ")\n";
return 0;
}
Output
1 5 6 (size 3)
How It Works
std::next(l.begin(), 1) points at 2, and std::next(l.begin(), 4) points at 5.
The range [first, last) includes 2 3 4 but stops before 5, so exactly three nodes are erased.
The elements 1, 5, and 6 are left, and the size drops from six to three.
Example 3 — erase() Inside a Loop
Remove all even numbers while walking the list. The loop uses the iterator returned by erase().
C++
#include <iostream>
#include <list>
int main() {
std::list<int> l = {1, 2, 3, 4, 5, 6};
for (auto it = l.begin(); it != l.end(); ) {
if (*it % 2 == 0) {
it = l.erase(it);
} else {
++it;
}
}
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
1 3 5
How It Works
The for loop has no ++it in its header, because the step depends on what the loop body does.
For an even number, it = l.erase(it) removes it and jumps to the next node. For an odd number, ++it moves on normally.
If the loop used ++it after erase(it), it would use an erased iterator, which is undefined behavior. The result here is 1 3 5.
Frequently Asked Questions
It removes one element, or a range of elements, from the list. You give the element or range as iterators. It returns an iterator to the element after the erased ones.
An iterator to the element that follows the last erased element. If you erased the last element, it returns end().
No. Only iterators that point to the erased elements become invalid. This makes a list a good choice when you keep many iterators around.
erase() works with a position (iterators). remove() works with a value and erases every element equal to that value.
A list has no index, so move an iterator first: l.erase(std::next(l.begin(), 2)) erases the third element. Make sure the index is smaller than size().