Open In App

list::remove() and list::remove_if() in C++ STL

Improve
Improve
Like Article
Like
Save
Share
Report

Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists.
 

list::remove()

remove() function is used to remove all the values from the list that correspond to the value given as parameter to the function
Syntax : 
 

listname.remove(value)
Parameters :
The value of the element to be removed is passed as the parameter.
Result :
Removes all the elements of the container
equal to the value passed as parameter

Examples: 
 

Input  : list list{1, 2, 3, 4, 5};
         list.remove(4);
Output : 1, 2, 3, 5

Input  : list list{1, 2, 2, 2, 5, 6, 7};
         list.remove(2);
Output : 1, 5, 6, 7

Errors and Exceptions 
 

  1. Shows error if the value passed doesn’t match the list type.
  2. Shows no exception throw guarantee if the comparison between value and elements of the list feature doesn’t throw any exception.

 

CPP




// CPP program to illustrate
// Implementation of remove() function
#include <iostream>
#include <list>
using namespace std;
 
int main()
{
    list<int> mylist{ 1, 2, 2, 2, 5, 6, 7 };
    mylist.remove(2);
    for (auto it = mylist.begin(); it != mylist.end(); ++it)
        cout << ' ' << *it;
}


Output: 
 

1 5 6 7

Time Complexity – Linear O(N)

Space Complexity: O(n) where n is size of the list

list::remove_if()

remove_if() function is used to remove all the values from the list that correspond true to the predicate or condition given as parameter to the function. The function iterates through every member of the list container and removes all the element that return true for the predicate.
Syntax : 
 

listname.remove_if(predicate)
Parameters :
The predicate in the form of aa function pointer
or function object is passed as the parameter.
Result :
Removes all the elements of the container
which return true for the predicate.

Examples: 
 

Input  : list list{1, 2, 3, 4, 5};
         list.remove_if(odd);
Output : 2, 4

Input  : list list{1, 2, 2, 2, 5, 6, 7};
         list.remove_if(even);
Output : 1, 5, 7

Errors and Exceptions 
 

  1. Shows no exception throw guarantee if the predicate function feature doesn’t throw any exception.

 

CPP




// CPP program to illustrate
// Implementation of remove_if() function
#include <iostream>
#include <list>
using namespace std;
 
// Predicate implemented as a function
bool even(const int& value) { return (value % 2) == 0; }
 
// Main function
int main()
{
    list<int> mylist{ 1, 2, 2, 2, 5, 6, 7 };
    mylist.remove_if(even);
    for (auto it = mylist.begin(); it != mylist.end(); ++it)
        cout << ' ' << *it;
}


Output: 
 

1 5 7

Application : Given a list of integers, remove all the prime numbers from the list and print the list. 
 

Input  : 2, 4, 6, 7, 9, 11, 13
Output : 4, 6, 9

 

CPP




// CPP program to illustrate
// Application of remove_if() function
#include <iostream>
#include <list>
using namespace std;
 
// Predicate implemented as a function
bool prime(const int& value)
{
    int i;
    for (i = 2; i < value; i++) {
        if (value % i == 0) {
            return false;
            
        }
    }
    if (value == i) {
        return true;
    }
}
 
// Main function
int main()
{
    list<int> mylist{ 2, 4, 6, 7, 9, 11, 13 };
    mylist.remove_if(prime);
    for (auto it = mylist.begin(); it != mylist.end(); ++it)
        cout << ' ' << *it;
}


Output: 
 

4 6 9

 Time Complexity: O(n)

Auxiliary Space: O(n) where n is size of the given list



Last Updated : 07 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads