共用方式為


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 類別

標準樣板程式庫