C++ std::list erase() Function

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

Definition

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)
CallWhat 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).

Library declarations
C++
iterator erase(const_iterator pos);
iterator erase(const_iterator first, const_iterator last);

Parameters

ParameterDescription
posThe iterator of the single element to erase. It must be valid and not end().
first, lastThe range to erase. first is included, last is not. last may be end().

Return Value

Return ValueDescription
iteratorAn 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;
}

How It Works

  1. std::next(l.begin()) gives an iterator to the second node, which holds 20.
  2. erase(it) unlinks that node. The old it is now invalid, so we replace it with the returned iterator.
  3. 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;
}

How It Works

  1. std::next(l.begin(), 1) points at 2, and std::next(l.begin(), 4) points at 5.
  2. The range [first, last) includes 2 3 4 but stops before 5, so exactly three nodes are erased.
  3. 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;
}

How It Works

  1. The for loop has no ++it in its header, because the step depends on what the loop body does.
  2. For an even number, it = l.erase(it) removes it and jumps to the next node. For an odd number, ++it moves on normally.
  3. 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().

Next: front()

Read or change the first element with std::list.

front() 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