lower_bound() ,up_bound()

头文件#include <algorithm>


  ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, last)中的第一个大于等于值val的位置。

     ForwardIter upper_bound(ForwardIter first, ForwardIter last, const _Tp& val)算法返回一个非递减序列[first, last)中第一个大于val的位置。

(其实是返回的迭代器)

     lower_bound和upper_bound如下图所示:

 


举个简单的例子


#include <iostream>  
#include <algorithm>//必须包含的头文件  
using namespace std;  
int main(){  
 int point[10] = {1,3,7,7,9};  
 int tmp = upper_bound(point, point + 5, 7) - point;//按从小到大,7最多能插入数组point的哪个位置  
 printf("%dn",tmp);  
 tmp = lower_bound(point, point + 5, 7) - point;按从小到大,7最少能插入数组point的哪个位置  
 printf("%dn",tmp);  
 return 0;  
}  
//output:  
//4  
//2  

还有很多的例子啊哈

点击打开链接

ss 

#include<iostream>  
#include<algorithm>  
using namespace std;  
int main()  
{  
    int a[]={0,1,2,4,5,6,7,8,9,10};  
    //返回对应元素  
    cout<<*upper_bound(a,a+10,3)<<endl;   //4 第一个大于  
  
  
    cout<<*lower_bound(a,a+10,3)<<endl;   //4 第一个大于等于  
  
  
    //返回对应下标  
    cout<<upper_bound(a,a+10,4)-a<<endl;   //4 第一个大于  
  
  
    cout<<lower_bound(a,a+10,4)-a<<endl;   //3 第一个大于等于  
  
  
    cout<<upper_bound(a,a+10,-1)-a<<endl;   //0 未找到将返回第一个  
  
  
    cout<<lower_bound(a,a+10,-1)-a<<endl;   //0 未找到将返回第一个  
  
  
    cout<<upper_bound(a,a+10,11)-a<<endl;   //10 未找到将返回最后  
  
  
    cout<<lower_bound(a,a+10,11)-a<<endl;   //10 未找到将返回最后  
    /* 
    注意: 
    两个函数使用时,必须确定序列为非递减序列,否则会发生错误 
    */  
    return 0;  
}  

下面再举一个栗子嘿嘿嘿:

这两个函数还分别有一个重载函数,可以接受第四个参数。如果第四个参数传入greater<Type>(),其中Type改成对应类型,可以用于查找非递增序列

测试样例

#include<iostream>  
#include<algorithm>  
using namespace std;  
int main()  
{  
    int a[]={10,9,8,7,6,5,4,2,1,0};  
  
    cout<<upper_bound(a,a+10,4,greater<int>())-a<<endl;   //7 返回第一个小于  
  
    cout<<lower_bound(a,a+10,4,greater<int>())-a<<endl;   //6 返回第一个小于等于  
  
    cout<<upper_bound(a,a+10,-1,greater<int>())-a<<endl;   //10 未找到将返回最后  
  
    cout<<lower_bound(a,a+10,-1,greater<int>())-a<<endl;   //10 未找到将返回最后  
  
    cout<<upper_bound(a,a+10,11,greater<int>())-a<<endl;   //0 未找到将返回第一个位置  
  
    cout<<lower_bound(a,a+10,11,greater<int>())-a<<endl;   //0 未找到将返回第一个位置  
  
    return 0;  
} 

最后一个栗子:

 两个函数配合使用可以进行查找 有序序列中的元素个数

    #include<iostream>  
    #include<algorithm>  
    using namespace std;  
    int main()  
    {  
        int a[]={0,1,2,3,5,5,5,6,7};  
       //查找元素 5 的个数  
        cout<<upper_bound(a,a+10,5)-lower_bound(a,a+10,5)<<endl;  
      
        return 0;  
    }  

暂完over。。。

不行,那就顺路再加一个例题好了,之前做过的  最长上升子序的问题,用lower_bound()会做的更简单

将小的数存进去,然后全部存进去之后,第一个INF就是最长的那一个串的长度

void solve2()
{
    fill(dp,dp+n,INF);
    for(int i=0;i<n;i++)
       *lower_bound(dp,dp+n,a[i])=a[i];
    printf("%d\n",lower_bound(dp,dp+n,INF)-dp);
}



  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值