lower_bound( begin,end,value):从数组的begin位置到end-1位置二分查找第一个大于或等于value的数字,如果是数组,则返回该数字的指针,如果是容器,则返回该位置的迭代器,查询失败则返回end的指针或者迭代器。
upper_bound( begin,end,value):从数组的begin位置到end-1位置二分查找第一个大于value的数字,如果是数组,则返回该数字的指针,如果是容器,则返回该位置的迭代器,查询失败则返回end的指针或者迭代器。
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<functional>
using namespace std;
const int INF = 5e2 + 10;
const int maxn = 100010;
cmp(int a,int b){
return a>b;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif
//使用数组
// int sum[maxn];
// int i=0;
// while(i!=10)
// scanf("%d",&sum[i++]);
// sort(sum,sum+10);
// int* low = lower_bound(sum,sum+10,16);//寻找的是 大于等于 value的指针,那么该下标标的前一个就是小于value的下标的最大值。
// int* upp = upper_bound(sum,sum+10,16);//寻找的是 大于 value的指针,那么该下标标的前一个就是小于等于value的下标的最大值。
// printf("%d\n",low-sum);//如果说查找失败的 ,返回的就是可以插入的位置,判断该下标等不等于sum+10就行了。
// printf("%d",upp-sum);
//使用容器
vector<int> vt;
int in;
while(scanf("%d",&in)!=EOF){
//vt.insert(in);
vt.push_back(in);
}
vector<int>::iterator vi ;
sort(vt.begin(),vt.end());
// while(!vt.empty()){
// vi = vt.end()-1;
// printf("%d\n",*vi);
// vt.pop_back();
// }
for(vector<int>::iterator v = vt.begin();v!=vt.end();v++){
printf("%d\n",*v);
}
vector<int>::iterator low ,low1;
low = lower_bound(vt.begin(),vt.end(),10);
low1 = lower_bound(vt.begin(),vt.end(),10,greater<int>());//greater<int>()用来重载lower_bound(),
printf("%d\n",low-vt.begin()); //返回的是第一个小于等于valued的迭代器
printf("%d\n",low1-vt.begin()); //头文件是 #include<functional>
return 0;
}