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

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 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
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 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 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;
            ;
            break;
        }
    }
    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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.