1.查找:STL中关于二分查找的函数有三个lower_bound 、upper_bound 、binary_search 。这三个函数都运用于有序区间(当然这也是运用二分查找的前提),下面记录一下这两个函数。

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如下图所示:如果所有元素都小于val,则返回last的位置且last的位置是越界的!!~

2.插入:举例如下:

一个数组num序列为:4,10,11,30,69,70,96,100.设要插入数字3,9,20,111。pos为要插入的位置的下标,则

pos=lower_bound(num,num+8,3)-num,pos=0。即num数组的下标为0的位置。

pos=lower_bound(num,num+8,9)-num,pos=1。即num数组的下标为1的位置(即10所在的位置)。

pos=lower_bound(num,num+8,111)-num,pos=8。即num数组的下标为8的位置(但下标上限为7,所以返回最后一个元素的下一个元素)。

pos=upper_bound(num,num+8,20)-num,pos=3。即num数组中的下标为3的位置。

lower_bound()函数,upper_bound()函数的更多相关文章

  1. STL之std::set、std::map的lower_bound和upper_bound函数使用说明

    由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊 ...

  2. C++ lower_bound 与 upper_bound 函数

    头文件: #include  <algorithm> 二分查找的函数有 3 个: 参考:C++ lower_bound 和upper_bound lower_bound(起始地址,结束地址 ...

  3. lower_bound和upper_bound函数

    lower_bound(ForwardIter first,ForwardIter last,const_TP & val) upper_bound(ForwardIter first,For ...

  4. Maximum Value(unique函数,lower_bound()函数,upper_bound()函数的使用)

    传送门 在看大佬的代码时候遇到了unique函数以及二分查找的lower_bound和upper_bound函数,所以写这篇文章来记录以备复习. unique函数 在STL中unique函数是一个去重 ...

  5. C++二分查找:lower_bound( )和upper_bound( )

    #include<algorithm>//头文件 //标准形式 lower_bound(int* first,int* last,val); upper_bound(int* first, ...

  6. C++中lower_bound函数和upper_bound函数

    STL中关于二分查找的函数有三个lower_bound .upper_bound .binary_search .这三个函数都运用于有序区间(当然这也是运用二分查找的前提),下面记录一下这两个函数. ...

  7. 二分检索函数lower_bound()和upper_bound()

    二分检索函数lower_bound()和upper_bound() 一.说明 头文件:<algorithm> 二分检索函数lower_bound()和upper_bound() lower ...

  8. 奇思妙想:利用野指针和lower_bound()/upper_bound()函数实现整数二分

    众所周知,c++的STL中提供了三个二分查找函数,binary_search(),lower_bound(),upper_bound(),功能分别是找某值是否在数组中出现,找到数组中第一个大于等于某值 ...

  9. STL之lower_bound和upper_bound

    ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...

随机推荐

  1. ytu 1052: 写一函数,将两个字符串连接(水题,指针练习)

    1052: 写一函数,将两个字符串连接 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 343  Solved: 210[Submit][Status][ ...

  2. android硬件返回

    1.第一种 @Override    public boolean onKeyUp(int keyCode, KeyEvent event) {        //点击回退键        if(Ke ...

  3. [UIImage _isCached]: message sent to deallocated instance

    本文转载至 http://zhuhaibobb.blog.163.com/blog/static/2744006720124191633375/       这几天做了个APP打开20份钟左右就强制退 ...

  4. 修改tomcat服务器默认端口号

    打开tomcat目录下conf目录下的server.xml,里面会有下面这样一段代码: <Connector port="8080" protocol="HTTP/ ...

  5. Git-fatal: unable to access 'xxx' : Could not resolve host: xxx

    解决办法:(在知乎上找到 确实好用) 1.查询代理 git config --global http.proxy 2.取消代理设置 git config --global --unset http.p ...

  6. 用Java向数据库中插入大量数据时的优化

    使用jdbc向数据库插入100000条记录,分别使用statement,PreparedStatement,及PreparedStatement+批处理3种方式进行测试: public void ex ...

  7. Spring和quartz整合的入门使用教程

    Quartz的maven依赖 <!-- quartz 的jar --> <dependency> <groupId>org.quartz-scheduler< ...

  8. 云计算之路-阿里云上:对“黑色n秒”问题的最终猜想——CPU C-states引起的

    如果说2013年云计算之路的主题是“踩坑”,那么2014年我们希望云计算之路的主题变成“填坑”——当然填坑是阿里云来完成的,我们只是见证曾经的坑坑洼洼变成平坦大道. 15号(周四)晚上我们发现了SLB ...

  9. CodeForces 24D Broken robot (概率DP)

    D. Broken robot time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  10. 1044: Access denied for user 'hehe'@'localhost' to database 'imooc'

    当我使用 mysql授予用户时, GRANT ALL PRIVILEGES ON *.* TO hehe IDENTIFIED BY 'some' WITH GRANT OPTION; 出现:1044 ...