Skip to content
Related Articles

Related Articles

forward_list::remove() and forward_list::remove_if() in C++ STL
  • Last Updated : 27 Nov, 2018

Forward list in STL implements singly linked list. Introduced from C++11, forward list are useful than other containers in insertion, removal and moving operations (like sort) and allows time constant insertion and removal of elements.It differs from list by the fact that forward list keeps track of location of only next element while list keeps track to both next and previous elements.

forward_list::remove()

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

Syntax :

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

Examples:

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

Input : forward_list forwardlist{1, 2, 2, 2, 5, 6};
        forwardlist.remove(2);
Output :1, 5, 6

Errors and Exceptions



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




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


Output:

1 5 6 7
forward_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 :

forwardlistname.remove_if(predicate)
Parameters :
The predicate in the form of a 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  : forward_list forwardlist{1, 2, 3, 4, 5};
         forwardlist.remove_if(odd);
Output : 2, 4

Input  : forward_list forwardlist{1, 2, 2, 2, 5, 6, 7};
         forwardlist.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 <forward_list>
#include <iostream>
using namespace std;
  
// Predicate implemented as a function
bool even(const int& value) { return (value % 2) == 0; }
  
// Main function
int main()
{
    forward_list<int> myforwardlist{ 1, 2, 2, 2, 5, 6, 7 };
    myforwardlist.remove_if(even);
    for (auto it = myforwardlist.begin(); it != myforwardlist.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 <forward_list>
#include <iostream>
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()
{
    forward_list<int> myforwardlist{ 2, 4, 6, 7, 9, 11, 13 };
    myforwardlist.remove_if(prime);
    for (auto it = myforwardlist.begin(); it != myforwardlist.end(); ++it)
        cout << ' ' << *it;
}


Output

4 6 9

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :