C++ std::list::erase

The std::list::erase function removes elements from a std::list at the specified position or within a specified range. It reduces the size of the list and ensures that iterators to the erased elements are invalidated. The function is useful for removing single elements or a block of elements from the list.


Syntax of std::list::erase

</>
Copy
// 1. Erase a single element
iterator erase(const_iterator position);

// 2. Erase a range of elements
iterator erase(const_iterator first, const_iterator last);

Parameters

ParameterDescription
positionA constant iterator specifying the position of the element to erase.
first, lastIterators specifying the range of elements to erase. The range includes all elements from first to last (but not last).

Return Value

The function returns an iterator pointing to the element that follows the last erased element. If the last erased element is the last element of the list, the returned iterator is equal to end().

Exceptions

The std::list::erase function does not throw exceptions itself. However, invalid iterators or undefined ranges may lead to undefined behavior. Always ensure that the iterators are valid and within the list’s range.


Examples for std::list::erase

Example 1: Erasing a Single Element

This example demonstrates how to use erase to remove a single element from a list:

Program

</>
Copy
#include <iostream>
#include <list>

int main() {
    std::list<int> myList = {10, 20, 30, 40};

    // Erase the second element
    myList.erase(++myList.begin());

    std::cout << "List contents after erase: ";
    for (const auto& elem : myList) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}

Explanation:

  1. A std::list named myList is initialized with the elements {10, 20, 30, 40}.
  2. The erase function is called with ++myList.begin(), which points to the second element (20).
  3. The element 20 is removed, and the list now contains {10, 30, 40}.
  4. A range-based for loop iterates through the list and prints its contents.

Output:

List contents after erase: 10 30 40

Example 2: Erasing a Range of Elements

This example demonstrates how to use erase to remove a range of elements from a list:

Program

</>
Copy
#include <iostream>
#include <list>

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

    // Erase elements from the second to the fourth
    myList.erase(++myList.begin(), --myList.end());

    std::cout << "List contents after erase: ";
    for (const auto& elem : myList) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}

Explanation:

  1. A std::list named myList is initialized with the elements {10, 20, 30, 40, 50}.
  2. The erase function is called with a range defined by ++myList.begin() (pointing to 20) and --myList.end() (pointing to 40).
  3. The elements 20, 30, and 40 are removed, leaving {10, 50}.
  4. A range-based for loop iterates through the list and prints its contents.

Output:

List contents after erase: 10 50

Example 3: Handling Erase with an Empty List

This example demonstrates how to safely use erase with an empty list to avoid undefined behavior:

Program

</>
Copy
#include <iostream>
#include <list>

int main() {
    std::list<int> myList;

    if (!myList.empty()) {
        myList.erase(myList.begin());
    } else {
        std::cout << "List is empty, cannot erase elements." << std::endl;
    }

    return 0;
}

Explanation:

  1. A std::list named myList is created and is initially empty.
  2. The empty() method is used to check if the list contains elements before attempting to call erase.
  3. If the list is empty, a message is printed to indicate that the operation cannot be performed.

Output:

List is empty, cannot erase elements.