list::unique
移除相邻重复满足从列表的一些其他二进制谓词的元素或相邻元素。
void unique( );
template<class BinaryPredicate>
void unique(
BinaryPredicate _Pred
);
参数
- _Pred
用于的二进制谓词比较连续的元素。
备注
此功能,假设列表进行排序,因此,所有重复元素是相邻的。 重复不是相邻的不会被删除。
第一个成员函数移除与其前面的元素相等的每个元素。
第二个成员函数移除与其前面的元素比较,满足谓词函数 _Pred 的每个元素。 在 **<functional>**标头可用来声明的任何二进制函数对象为_Pred的参数或可以创建您的常数。
示例
// 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