直接代码如下:
//includes
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
bool jiayibijiao(int a,int b){
return a==b;
}
int main(){
int a[]={1,2,3,4,5,6,7,8,9,10};
int b[]={5,6,7,8,9};
vector<int>v(a,a+10);//调用includes前要先排序
vector<int>vv(b,b+5);
sort(v.begin(),v.end());
sort(vv.begin(),vv.end());
if(includes(v.begin(),v.end(),vv.begin(),vv.end())){
cout<<"v includes vv"<<endl;
}else{
cout<<"v not includes vv"<<endl;
}
sort(v.begin(),v.end());
sort(vv.begin(),vv.end());
if(includes(v.begin(),v.end(),vv.begin(),vv.end(),jiayibijiao)){
cout<<"v includes vv+0"<<endl;
}else{
cout<<"v not includes vv+0"<<endl;
}
system("pause");
return 0;
}
//lower_bound(),upper_bound()
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
bool nothing(int a,int b){
return a<b;
}
int main(){
int a[]={1,3,4,5,5,5,6,7,8,9};
vector<int>v(a,a+10);
sort(v.begin(),v.end());//调用函数前要先排序
vector<int>::iterator iter=lower_bound(v.begin(),v.end(),5);
cout<<*iter<<endl;//指向第一个满足条件的元素
iter=upper_bound(v.begin(),v.end(),5);
cout<<*iter<<endl;//指向第一个不满足条件的元素
vector<int>::iterator it=lower_bound(v.begin(),v.end(),5,nothing);
cout<<"the first location:"<<distance(v.begin(),it)<<endl;
it=upper_bound(v.begin(),v.end(),5,nothing);
cout<<"the second location:"<<distance(v.begin(),it)<<endl;
system("pause");
return 0;
}
//equal_range
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
int main(){
int a[]={7,4,1,8,5,2,9,6,3,5,4,5,4,5,5};
vector<int>v(a,a+15);
sort(v.begin(),v.end());//先排序
pair<vector<int>::iterator,vector<int>::iterator>fanwei;
fanwei=equal_range(v.begin(),v.end(),5);//关联函数设置了这个成员函数,效果更佳
cout<<"the first loaction:"<<distance(v.begin(),fanwei.first)<<endl;
cout<<*(fanwei.first)<<endl;
cout<<"the second location:"<<distance(v.begin(),fanwei.second)<<endl;
cout<<*(fanwei.second)<<endl;
system("pause");
return 0;
}