unique:
Removes duplicate elements that are adjacent to each other in a specified range.
unique函数
#include<vector>
#include<algorithm>
#include<functional>
#include<iostream>
#include<ostream>
usingnamespacestd;
//Return whether modulus of elem1 is equal to modulus of elem2
boolmod_equal (intelem1,intelem2 )
{
if( elem1<0)
elem1=-elem1;
if( elem2<0)
elem2=-elem2;
returnelem1==elem2;
};
intmain( )
{
vector<int>v1;
vector<int>::iterator v1_Iter1, v1_Iter2, v1_Iter3,
v1_NewEnd1, v1_NewEnd2, v1_NewEnd3;
inti;
for( i=0; i<=3; i++)
{
v1.push_back(5);
v1.push_back(-5);
}
intii;
for( ii=0; ii<=3; ii++)
{
v1.push_back(4);
}
v1.push_back(7);
cout<<"Vector v1 is (";
for( v1_Iter1=v1.begin( ) ; v1_Iter1!=v1.end( ) ; v1_Iter1++)
cout<<v1_Iter1<<"";
cout<<")."<<endl;
//Remove consecutive duplicates
v1_NewEnd1=unique ( v1.begin ( ) , v1.end ( ) );
cout<<"Removing adjacent duplicates from vector v1 givesn (";
for( v1_Iter1=v1.begin( ) ; v1_Iter1!=v1_NewEnd1 ; v1_Iter1++)
cout<<v1_Iter1<<"";
cout<<")."<<endl;
//Remove consecutive duplicates under the binary prediate mod_equals
v1_NewEnd2=unique ( v1.begin ( ) , v1_NewEnd1 , mod_equal );
cout<<"Removing adjacent duplicates from vector v1 under then"
<<"binary predicate mod_equal givesn (";
for( v1_Iter2=v1.begin( ) ; v1_Iter2!=v1_NewEnd2 ; v1_Iter2++)
cout<<v1_Iter2<<"";
cout<<")."<<endl;
//Remove elements if preceded by an element that was greater
v1_NewEnd3=unique ( v1.begin ( ) , v1_NewEnd2, greater<int>( ) );
cout<<"Removing adjacent elements satisfying the binaryn"
<<"predicate mod_equal from vector v1 gives (";
for( v1_Iter3=v1.begin( ) ; v1_Iter3!=v1_NewEnd3 ; v1_Iter3++)
cout<<v1_Iter3<<"";
cout<<")."<<endl;
}
sort:
Arranges the elements in a specified range into a nondescending order or according to an ordering criterion specified by a binary predicate.
代码#include<vector>
#include<algorithm>
#include<functional>//For greater
#include<iostream>
//Return whether first element is greater than the second
boolUDgreater (intelem1,intelem2 )
{
returnelem1>elem2;
}
intmain( )
{
usingnamespacestd;
vector<int>v1;
vector<int>::iterator Iter1;
inti;
for( i=0; i<=5; i++)
{
v1.push_back(2i );
}
intii;
for( ii=0; ii<=5; ii++)
{
v1.push_back(2ii+1);
}
cout<<"Original vector v1 = (";
for( Iter1=v1.begin( ) ; Iter1!=v1.end( ) ; Iter1++)
cout<<Iter1<<"";
cout<<")"<<endl;
sort( v1.begin( ), v1.end( ) );
cout<<"Sorted vector v1 = (";
for( Iter1=v1.begin( ) ; Iter1!=v1.end( ) ; Iter1++)
cout<<Iter1<<"";
cout<<")"<<endl;
//To sort in descending order. specify binary predicate
sort( v1.begin( ), v1.end( ), greater<int>( ) );
cout<<"Resorted (greater) vector v1 = (";
for( Iter1=v1.begin( ) ; Iter1!=v1.end( ) ; Iter1++)
cout<<Iter1<<"";
cout<<")"<<endl;
//A user-defined (UD) binary predicate can also be used
sort( v1.begin( ), v1.end( ), UDgreater );
cout<<"Resorted (UDgreater) vector v1 = (";
for( Iter1=v1.begin( ) ; Iter1!=v1.end( ) ; Iter1++)
cout<<Iter1<<"";
cout<<")"<<endl;
}
binary_search
Tests whether there is an element in a sorted range that is equal to a specified value or that is equivalent to it in a sense specified by a binary predicate.
代码#include<list>
#include<vector>
#include<algorithm>
#include<iostream>
//Return whether modulus of elem1 is less than modulus of elem2
boolmod_lesser (intelem1,intelem2 )
{
if(elem1<0)
elem1=-elem1;
if(elem2<0)
elem2=-elem2;
returnelem1<elem2;
}
intmain( )
{
usingnamespacestd;
list<int>L;
list<int>::iterator Iter;
boolb1, b2;
L.push_back(50);
L.push_back(10);
L.push_back(30);
L.push_back(20);
L.push_back(25);
L.push_back(5);
L.sort( );
cout<<"L = (";
for( Iter=L.begin( ) ; Iter!=L.end( ) ; Iter++)
cout<<Iter<<"";
cout<<")"<<endl;
b1=binary_search( L.begin( ), L.end( ),10);
if( b1 )
cout<<"There is an element in list L with a value equal to 10."
<<endl;
else
cout<<"There is no element in list L with a value equal to 10."
<<endl;
//a binary_search under the binary predicate greater
L.sort ( greater<int>( ) );
b2=binary_search( L.begin( ), L.end( ),10, greater<int>( ) );
if( b2 )
cout<<"There is an element in list L with a value equivalent to 10"
<<"under greater than."<<endl;
else
cout<<"No element in list L with a value equivalent to 10"
<<"under greater than."<<endl;
//a binary_search under the user-defined binary predicate mod_lesser
vector<int>v1;
vector<int>::iterator Iter1;
inti;
for( i=-2; i<=4; i++)
{
v1.push_back( i );
}
sort ( v1.begin ( ) , v1.end ( ) , mod_lesser );
cout<<"Ordered under mod_lesser, vector v1 = (";
for( Iter1=v1.begin( ) ; Iter1!=v1.end( ) ; Iter1++)
cout<<Iter1<<"";
cout<<")"<<endl;
boolb3=binary_search( v1.begin( ), v1.end( ),-3, mod_lesser );
if( b3 )
cout<<"There is an element with a value equivalent to -3"
<<"under mod_lesser."<<endl;
else
cout<<"There is not an element with a value equivalent to -3"
<<"under mod_lesser."<<endl;
}
原文链接: https://www.cnblogs.com/answeryi/archive/2010/10/10/1847249.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/15954
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!