list::remove_if
Usuwa elementy z listy, dla którego upewni się określony predykatu.
template<class Predicate>
void remove_if(
Predicate _Pred
)
Parametry
- _Pred
Predykatu jednoelementowego co, jeśli spełnione przez element, powoduje usunięcie elementu z listy.
Przykład
// list_remove_if.cpp
// compile with: /EHsc
#include <list>
#include <iostream>
template <class T> class is_odd : public std::unary_function<T, bool>
{
public:
bool operator( ) ( T& val )
{
return ( val % 2 ) == 1;
}
};
int main( )
{
using namespace std;
list <int> c1;
list <int>::iterator c1_Iter, c2_Iter;
c1.push_back( 3 );
c1.push_back( 4 );
c1.push_back( 5 );
c1.push_back( 6 );
c1.push_back( 7 );
c1.push_back( 8 );
cout << "The initial list is c1 =";
for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
cout << " " << *c1_Iter;
cout << endl;
list <int> c2 = c1;
c2.remove_if( is_odd<int>( ) );
cout << "After removing the odd elements, "
<< "the list becomes c2 =";
for ( c2_Iter = c2.begin( ); c2_Iter != c2.end( ); c2_Iter++ )
cout << " " << *c2_Iter;
cout << endl;
}
Wymagania
Nagłówek: <list>
Obszar nazw: std