Delete elements in C++ STL list Last Updated : 23 Jul, 2025 Suggest changes Share Like Article Like Report 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). CPP // C++ code to demonstrate the working of erase() #include<iostream> #include<list> // for list operations using namespace std; int main() { // initializing list of integers list<int> list1={10,15,20,25,30,35}; // declaring list iterators list<int>::iterator it = list1.begin(); list<int>::iterator it1 = list1.begin(); // incrementing the positions of iterators advance(it,2); advance(it1,5); // printing original list cout << "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 20 list1.erase(it); // list after deletion 1 element cout << "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 iterators advance(it,2); // using erase() to erase multiple elements // erases 25,30 list1.erase(it,it1); // list after deletion of multiple elements cout << "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) CPP // C++ code to demonstrate the working of pop_front() // and pop_back() #include<iostream> #include<list> // for list operations using namespace std; int main() { // initializing list of integers list<int> list1={10,15,20,25,30,35}; // printing original list cout << "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 10 list1.pop_front(); // list after deleting first element cout << "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 35 list1.pop_back(); // list after deleting last element cout << "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 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) CPP // C++ code to demonstrate the working of remove() // remove_if() #include<iostream> #include<list> // for list operations using namespace std; // function to pass in argument of "remove_if()" bool is_div_5(const int& num) { return num%5==0;} int main() { // initializing list of integers list<int> list1={10,14,20,22,30,33,22}; // printing original list cout << "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 22 list1.remove(22); // list after deleting all 22 occurrences cout << "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 5 list1.remove_if(is_div_5); // list after deleting all multiples of 5 cout << "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 K kartik Article Tags : C++ STL cpp-list Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++4 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++4 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL3 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like