直接代码如下:
//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;
}
分享到:
相关推荐
bound函数用法及应用领域和案例分享lower_bound函数用法及应用领域和案例分享lower_bound函数用法及应用领域和案例分享lower_bound函数用法及应用领域和案例分享lower_bound函数用法及应用领域和案例分享lower_...
二分查找及其变种,c++ upper_bound,c++ lower_bound(csdn)————程序
lower_bound函数是C++ STL中的一个函数,用于在有序序列中查找第一个大于或等于给定值的元素的位置。lower_bound函数接受两个参数,第一个参数是指向序列起始位置的迭代器,第二个参数是要查找的值。lower_bound函数...
lower_bound函数
lower_bound函数lower_bound函数&代码示例.zip
lower_bound函数lower_bound函数&代码示例.pdf
lower_bound函数
lower_bound函数
lower_bound函数
lower_bound 的简要介绍.docx
lower_bound函数
lower_bound是一个在C++标准库中的算法,它主要用于在有序序列中查找第一个不小于(即大于或等于)给定值的元素。其返回的是一个迭代器,指向这个元素。如果序列中没有这样的元素,lower_bound将返回一个指向序列...
lower_bound函数
matlab代码GPU_implementation_of_Tanner_lower_bound Matlab GPU/CPU 实现代码距离(代码字和 TS 伪代码字)的 Fundamental Tanner 下界估计。 估计代码距离的下限和启发式优化 second_eigen/first_eigen 以选择...
lower_bound函数
举例让你明白lower_bound函数
lower_bound函数代码(C++实现).rar
`lower_bound` 是 C++ 标准库 `<algorithm>` 中提供的一个函数,它主要用于在有序序列中查找给定值的插入位置,以保持序列的有序性。
Estimating the φ(n) of Upper_Lower Bound in its RSA Cryptosystem
代码包含set使用中的size,insert,count,find,erase,swap,lower_bound,upper_bound,equal_range方法以及详细例子,并设立类和对象,可以看出set如何对对象进行排序和其他操作。