1. unique( b, e ) //删除连续的重复的数据。留下唯一
2. unique( b, e, p )
3. unique_copy( b1, e1, b2 )
4. unique_copy( b1, e1, b2, p )
注意:
1.应该有一个unique_if(), 但是没有
2.应该有一个unique_copy_if(),但是没有
#include <iostream>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <iterator>
using namespace std;
bool differenceOne( int elem1, int elem2)
{
return elem1+1 == elem2 || elem1-1 == elem2;
}
template <typename T>
void Print( const T& t )
{
for(typename T::const_iterator itr=t.begin(); itr!=t.end(); ++itr)
{
cout<<*itr<<' ';
}cout<<endl;
}
int main()
{
int array[] = {1,4,4,6,1,2,2,3,1,6,6,6,5,7,5,4,4};
int arrayNum = sizeof(array)/sizeof(array[0]);
list<int> lst;
copy(array, array+arrayNum, back_inserter(lst) );
Print(lst);
list<int>::iterator pos;
pos = unique(lst.begin(), lst.end());
for(list<int>::iterator itr=lst.begin(); itr!=pos; ++itr)
{
cout<<*itr<<' ';
}cout<<endl;
copy(array, array+arrayNum, lst.begin() );
Print(lst);
pos = unique(lst.begin(), lst.end(), greater<int>() );
for(list<int>::iterator itr=lst.begin(); itr!=pos; ++itr)
{
cout<<*itr<<' ';
}cout<<endl;
copy(array, array+arrayNum, lst.begin() );
Print(lst);
unique_copy(lst.begin(), lst.end(), ostream_iterator<int>(cout, " "));
cout<<endl;
copy(array, array+arrayNum, lst.begin() );
Print(lst);
unique_copy(lst.begin(), lst.end(), ostream_iterator<int>(cout, " "), differenceOne);
cout<<endl;
return 0;
}