The Wayback Machine - https://web.archive.org/web/20240711052846/https://www.geeksforgeeks.org/list-erase-function-in-c-stl/
Open In App

list erase() function in C++ STL

Last Updated : 26 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

  • position: This parameter is used when the function is used to delete a single element. This parameter refers to an iterator which points to the element which is need to be erased from the list container.
  • first, last: These two parameters are used when the list is used to erase elements from a range. The parameter first refers to the iterator pointing to the first element in the range and the parameter last refers to the iterator pointing to the last element in the range which is needed to be erased. This erases all the elements in the range including the element pointed by the iterator first but excluding the element pointed by the iterator last.

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




// 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




// 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)



Previous Article
Next Article

Similar Reads

Erase Range of Elements From List Using Iterators in C++ STL
Prerequisites:List in C++Iterators in C++ A list is a type of container which requires the same properties as a doubly linked list. We can insert elements from either side of the list, but accessing elements with an index is not possible in the list. So, removing elements from the list is not an easy task. But, we have a method to remove multiple e
2 min read
unordered_set erase() function in C++ STL
The unordered_set::erase() function is a built-in function in C++ STL which is used to remove either a single element or a group of elements ranging from start(inclusive) to end(exclusive). This decreases the size of a container by the number of elements removed.Note: Buckets in unordered_set are numbered from 0 to n-1, where n is the total number
3 min read
unordered_multiset erase() function in C++ STL
The unordered_multiset::erase() function is a built-in function in C++ STL which is used to remove either a single element or, all elements with a definite value or, a range of elements ranging from start(inclusive) to end(exclusive). This decreases the size of the container by the number of elements removed. Syntax: unordered_multiset_name.erase(i
2 min read
map erase() function in C++ STL
map::erase() is a built-in function in C++ STL that is used to erase elements from the container. It can be used to erase keys and elements at any specified position or a given range. The syntax for erasing a key:map_name.erase(key)Parameters: The function accepts one mandatory parameter key which specifies the key to be erased in the map container
4 min read
deque::clear() and deque::erase() in C++ STL
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::clear() The clear()
5 min read
multimap::erase() in C++ STL
multimap::erase() is a built-in function in C++ STL which is used to erase element from the container. It can be used to erase keys, elements at any specified position or a given range. Syntax for erasing a key: multimap_name.erase(key) Parameters: The function accepts one mandatory parameter key which specifies the key to be erased in the multimap
4 min read
unordered_map erase in C++ STL
erase function is used to erase elements from the unordered_map. There are three type of erase functions supported by unordered_map : erasing by iterator: It takes an iterator as a parameter and erases the key and value present at that iterator. Syntax unordered_map.erase(const iterator); erasing by key: It takes a key as a parameter and erases the
2 min read
unordered_multimap erase in C++ STL
unordered_multimap::erase() is a built in function in C++ STL which removes the element from given range, by position and by key. There are three variant of this function in C++ STL. There are following type of erase() functions in C++ for unordered_multimap. By position : It removes element from unordered_multimap by given position and return an i
4 min read
set::erase in C++ STL
Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::erase()erase() function is used to remove elements from a containe
5 min read
Erase-Remove Idiom in C++
Erase-Remove-Idiom is a C++ STL (Standard Template Library) technique to remove elements from a container. It is used to remove all the elements satisfying certain conditions from the container. The erase-remove idiom is especially useful in array-based containers like vectors, where each elimination requires all the remaining elements to adjust. I
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg