Forward list in STL implements singly linked list. The forward list was introduced in C++11, and is useful than other containers in insertion, removal, and moving operations (like sort) and allows time constant insertion and removal of elements. It differs from the list by the fact that the forward list keeps track of the location of only the next element while the list keeps track of both the 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 a parameter to the function. This function comes under the <forward_list> header file.
Syntax:
forwardlistname.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 a parameter.
Time Complexity: Linear in container size.
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:
- Shows an error if the value passed doesn’t match the forward list type.
- Shows no exception throw guarantee if the comparison between value and elements of the forward list feature doesn’t throw any exception.
CPP
#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;
return 0;
}
|
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 a parameter to the function. The function iterates through every member of the list container and removes all the elements that return true for the predicate. This function comes under the <forward_list> header file.
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.
Time Complexity: Linear in container size.
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: Shows no exception throw guarantee if the predicate function feature doesn’t throw any exception.
CPP
#include <forward_list>
#include <iostream>
using namespace std;
bool even( const int & value) { return (value % 2) == 0; }
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;
return 0;
}
|
Application of remove_if(): 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
#include <forward_list>
#include <iostream>
using namespace std;
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 ;
;
}
}
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;
return 0;
}
|
Let us see the differences in a tabular form -:
| forward_list::remove() | forward_list::remove_if() |
1. | It is used to remove from the container all the elements that compare equal to val. | It is used to remove from the container all the elements for which Predicate pred returns true. |
2. | Its syntax is -: remove (const value_type& val); | Its syntax is -: remove_if (Predicate pred); |
3. | Its return value is void type. | Its return value is void type. |
4. | It takes one parameter which is the value of the elements to be removed. | It takes one parameter that is Unary predicate |
5. | Its complexity is linar. | Its complexity is linar. |
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.