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