list::unique
隣接する重複要素、または他のいずれかの二項述語の条件を満たす隣接する要素を list から削除します。
void unique( ); template<class BinaryPredicate> void unique( BinaryPredicate _Pred );
パラメーター
- _Pred
一連の要素の比較に使用する二項述語。
解説
この関数は、すべての重複要素が隣接するように list が並べ替えられていることを前提とします。 隣接していない重複要素は削除されません。
1 つ目のメンバー関数は、その直前の要素に一致するすべての要素を削除します。
2 つ目のメンバー関数は、直前の要素と比較したときに述語関数 _Pred の条件を満たすすべての要素を削除します。 引数 _Pred には、<functional> ヘッダー内で宣言したいずれかの二項関数オブジェクトを使用するか、独自の二項関数オブジェクトを作成できます。
使用例
// list_unique.cpp
// compile with: /EHsc
#include <list>
#include <iostream>
int main( )
{
using namespace std;
list <int> c1;
list <int>::iterator c1_Iter, c2_Iter,c3_Iter;
not_equal_to<int> mypred;
c1.push_back( -10 );
c1.push_back( 10 );
c1.push_back( 10 );
c1.push_back( 20 );
c1.push_back( 20 );
c1.push_back( -10 );
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.unique( );
cout << "After removing successive duplicate elements, c2 =";
for ( c2_Iter = c2.begin( ); c2_Iter != c2.end( ); c2_Iter++ )
cout << " " << *c2_Iter;
cout << endl;
list <int> c3 = c2;
c3.unique( mypred );
cout << "After removing successive unequal elements, c3 =";
for ( c3_Iter = c3.begin( ); c3_Iter != c3.end( ); c3_Iter++ )
cout << " " << *c3_Iter;
cout << endl;
}
必要条件
ヘッダー: <list>
名前空間: std