Share via


list::remove_if

指定した述語を満たすリストから要素を消去します。

template<class Predicate> 
void remove_if( 
   Predicate _Pred 
)

パラメーター

  • _Pred
    要素によって満たされる場合、単項述語は結果的にリストからその要素を削除します。

使用例

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

必要条件

ヘッダー: <list>

名前空間: std

参照

関連項目

list クラス

標準テンプレート ライブラリ