Delete elements in C++ STL list
How to insert elements in C++ STL List ?
This article covers the deletion aspects in STL list.
- Using list::erase(): The purpose of this function is to remove the elements from list. Single or multiple contiguous elements in range can be removed using this function. This function takes 2 arguments, start iterator and end iterator.
Time complexity : O(n) where (n is size of list).// C++ code to demonstrate the working of erase()#include<iostream>#include<list> // for list operationsusingnamespacestd;intmain(){// initializing list of integerslist<int> list1={10,15,20,25,30,35};// declaring list iteratorslist<int>::iterator it = list1.begin();list<int>::iterator it1 = list1.begin();// incrementing the positions of iteratorsadvance(it,2);advance(it1,5);// printing original listcout <<"The original list is : ";for(list<int>::iterator i=list1.begin(); i!=list1.end(); i++)cout << *i <<" ";cout << endl;// using erase() to erase single element// erases 20list1.erase(it);// list after deletion 1 elementcout <<"The list after deleting 1 element using erase() : ";for(list<int>::iterator i=list1.begin(); i!=list1.end(); i++)cout << *i <<" ";cout << endl;it = list1.begin();// incrementing the positions of iteratorsadvance(it,2);// using erase() to erase multiple elements// erases 25,30list1.erase(it,it1);// list after deletion of multiple elementscout <<"The list after deleting multiple elements using erase() : ";for(list<int>::iterator i=list1.begin(); i!=list1.end(); i++)cout << *i <<" ";cout << endl;}Output:
The original list is : 10 15 20 25 30 35 The list after deleting 1 element using erase() : 10 15 25 30 35 The list after deleting multiple elements using erase() : 10 15 35
- Using list::pop_front() and list::pop_back():
- pop_back() : This function removes the last element from the list. This reduces the size of list by 1.
Time complexity : O(1) - pop_front() : This function removes the first element from the list and shifts the subsequent elements. This reduces the size of list by 1.
Time complexity : O(1)
// C++ code to demonstrate the working of pop_front()// and pop_back()#include<iostream>#include<list> // for list operationsusingnamespacestd;intmain(){// initializing list of integerslist<int> list1={10,15,20,25,30,35};// printing original listcout <<"The original list is : ";for(list<int>::iterator i=list1.begin(); i!=list1.end(); i++)cout << *i <<" ";cout << endl;// using pop_front() to erase first element of list// pops 10list1.pop_front();// list after deleting first elementcout <<"The list after deleting first element using pop_front() : ";for(list<int>::iterator i=list1.begin(); i!=list1.end(); i++)cout << *i <<" ";cout << endl;// using pop_back() to erase last element of list// pops 35list1.pop_back();// list after deleting last elementcout <<"The list after deleting last element using pop_back() : ";for(list<int>::iterator i=list1.begin(); i!=list1.end(); i++)cout << *i <<" ";cout << endl;}Output:
The original list is : 10 15 20 25 30 35 The list after deleting first element using pop_front() : 15 20 25 30 35 The list after deleting last element using pop_back() : 15 20 25 30
- pop_back() : This function removes the last element from the list. This reduces the size of list by 1.
- Using remove() and remove_if():
- remove() : This function deletes all the occurrences of the value passed in its arguments. It is different from “erase()” from the fact that “erase()” deletes values by position, where as “remove()” deletes the value passed. The size of the list is reduced by the number of occurrences removed.
Time Complexity : O(n) - remove_if() : This function deletes the occurrences of the values that returns “true” to the function passed in its argument.
Time Complexity : O(n)
// C++ code to demonstrate the working of remove()// remove_if()#include<iostream>#include<list> // for list operationsusingnamespacestd;// function to pass in argument of "remove_if()"boolis_div_5(constint& num) {returnnum%5==0;}intmain(){// initializing list of integerslist<int> list1={10,14,20,22,30,33,22};// printing original listcout <<"The original list is : ";for(list<int>::iterator i=list1.begin(); i!=list1.end(); i++)cout << *i <<" ";cout << endl;// using remove() to delete all occurrences of 22list1.remove(22);// list after deleting all 22 occurrencescout <<"The list after deleting all 22 occurrences : ";for(list<int>::iterator i=list1.begin(); i!=list1.end(); i++)cout << *i <<" ";cout << endl;// using remove_if() to delete multiple of 5list1.remove_if(is_div_5);// list after deleting all multiples of 5cout <<"The list after deleting all multiples of 5 : ";for(list<int>::iterator i=list1.begin(); i!=list1.end(); i++)cout << *i <<" ";cout << endl;}Output:
The original list is : 10 14 20 22 30 33 22 The list after deleting all 22 occurrences : 10 14 20 30 33 The list after deleting all multiples of 5 : 14 33
- remove() : This function deletes all the occurrences of the value passed in its arguments. It is different from “erase()” from the fact that “erase()” deletes values by position, where as “remove()” deletes the value passed. The size of the list is reduced by the number of occurrences removed.
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.







Please Login to comment...