Open In App

list erase() function in C++ STL

The list::erase() is a built-in function in C++ STL which is used to delete elements from a list container. This function can be used to remove a single element or a range of elements from the specified list container.

Syntax: 



iterator list_name.erase(iterator position)

or,

iterator list_name.erase(iterator first, iterator last)

Parameters: This function can accepts different parameters based on whether it is used to erase a single element or a range of element from the list container.

Return Value: This function returns an iterator pointing to the element in the list container which followed the last element erased from the list container.



Below programs illustrates the list::erase() function.

Program 1: Erasing a single element. 




// CPP program to illustrate the
// list::erase() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // Creating a list
    list<int> demoList;
 
    // Add elements to the List
    demoList.push_back(10);
    demoList.push_back(20);
    demoList.push_back(30);
    demoList.push_back(40);
    demoList.push_back(50);
 
    // Printing elements of list before deleting
    // first element
    cout << "List before deleting first element: ";
    for (auto itr = demoList.begin();
        itr != demoList.end(); itr++) {
        cout << *itr << " ";
    }
 
    // Creating iterator to point to first
    // element in the list
    list<int>::iterator itr = demoList.begin();
 
    // deleting the first element
    demoList.erase(itr);
 
    // Printing elements of list after deleting
    // first element
    cout << "\nList after deleting first element:";
    for (auto itr = demoList.begin();
        itr != demoList.end(); itr++) {
        cout << *itr << " ";
    }
 
    return 0;
}

Output
List before deleting first element: 10 20 30 40 50 
List after deleting first element:20 30 40 50

Time complexity: O(n) 
Space complexity: O(1)

Program 2: Erasing a range of elements. 




// CPP program to illustrate the
// list::erase() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // Creating a list
    list<int> demoList;
 
    // Add elements to the List
    demoList.push_back(10);
    demoList.push_back(20);
    demoList.push_back(30);
    demoList.push_back(40);
    demoList.push_back(50);
 
    // Printing elements of list before deleting
    // any element
    cout << "List before deleting any element: ";
    for (auto itr = demoList.begin();
        itr != demoList.end(); itr++) {
        cout << *itr << " ";
    }
 
    // Creating iterators of the list
    list<int>::iterator itr1, itr2;
    itr1 = demoList.begin();
    itr2 = demoList.begin();
 
    // Incrementing itr2 by 3 positions
    advance(itr2, 3);
 
    // deleting range of elements from index [0, 3)
    demoList.erase(itr1, itr2);
 
    // Printing elements of list after deleting
    // range of elements from [0, 3)
    cout << "\nList after deleting first three elements: ";
    for (auto itr = demoList.begin();
        itr != demoList.end(); itr++) {
        cout << *itr << " ";
    }
 
    return 0;
}

Output
List before deleting any element: 10 20 30 40 50 
List after deleting first three elements: 40 50

Note: This function works in linear time complexity, that is the number of elements erased from the list container.

Time complexity: O(n) 
Space complexity: O(1)


Article Tags :
C++