lower_bound&&upper_bound用法简介

12 篇文章 0 订阅

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;                                                
    
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值