list::unique

从列表中删除满足某些其他二元谓词的相邻重复元素或相邻元素。

void unique( ); template<class BinaryPredicate>    void unique(       BinaryPredicate _Pred    );

参数

  • _Pred
    用于比较连续元素的二元谓词。

备注

此函数假设列表是经过排序的,因此所有重复元素都是相邻的。 不相邻的重复元素将不被删除。

第一个成员函数删除比较等于其前一个元素的每个元素。

第二个成员函数删除与前一个元素比较时满足谓词函数 _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

请参见

参考

list 类

标准模板库