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