Last Updated : 23 Jul, 2025
How to insert elements in C++ STL List ?This article covers the deletion aspects in STL list.
// 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
// 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
// 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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4