public member function template
<list>
std::list::remove_if
template <class Predicate>
void remove_if (Predicate pred);
Remove elements fulfilling condition
Removes from the container all the elements for which Predicate pred returns true. This calls the destructor of these objects and reduces the container size by the number of elements removed.
The function calls pred(*i) for each element (where i is an iterator to that element). Any of the elements in the list for which this returns true, are removed from the container.
Parameters
- pred
- Unary predicate that, taking a value of the same type as those contained in the forward_list object, returns true for those values to be removed from the container, and false for those remaining.
This can either be a function pointer or a function object.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
// list::remove_if
#include <iostream>
#include <list>
// a predicate implemented as a function:
bool single_digit (const int& value) { return (value<10); }
// a predicate implemented as a class:
struct is_odd {
bool operator() (const int& value) { return (value%2)==1; }
};
int main ()
{
int myints[]= {15,36,7,17,20,39,4,1};
std::list<int> mylist (myints,myints+8); // 15 36 7 17 20 39 4 1
mylist.remove_if (single_digit); // 15 36 17 20 39
mylist.remove_if (is_odd()); // 36 20
std::cout << "mylist contains:";
for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
| |
Output:
Complexity
Linear in list size (applications of pred).
Iterator validity
Iterators, pointers and references referring to elements removed by the function are invalidated.
All other iterators, pointers and reference keep their validity.
Data races
The container is modified.
The elements removed are modified. Concurrently accessing or modifying other elements is safe, although iterating through the container is not.
Exception safety
If pred is guaranteed to not throw, the function never throws exceptions (no-throw guarantee).
Otherwise, if an exception is thrown, the container is left in a valid state (basic guarantee).