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. 如何编写jQuery插件

    要说jQuery 最成功的地方,我认为是它的可扩展性吸引了众多开发者为其开发插件,从而建立起了一个生态系统.这好比大公司们争相做平台一样,得平台者得天下.苹果,微软,谷歌等巨头,都有各自的平台及生态圈 ...

  2. 【NLP】主题识别文档

    http://www.biostatistic.net/thread-94974-1-1.html http://www.doc88.com/p-9843685205530.html http://w ...

  3. uva 707(记忆化搜索)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21261 思路:此题需要记忆化搜索,dp[x][y][t]表示当前状 ...

  4. VS------快捷键一览

    1. 属性封装快捷键:Ctrl + R + E

  5. malloc free, new delete 的异同点

    相同点: 都可以动态的申请并释放内存 不同点: 1. 用法不同 <1> malloc 函数为 void* malloc(size_t size), 用于申请一块长度为 size 字节的内存 ...

  6. The type org.springframework.dao.DataAccessException cannot be resolved. It is indirectly referenced from required .class files

    使用spring框架提供的JDBC模板操作数据库时,提示错误 解决方案:导入事务管理jar包spring-tx-4.2.4.RELEASE.jar

  7. Spring中bean的作用范围

    singleton作用域: Spring的scope的默认值是singleton Spring 只会为每一个bean创建一个实例,并保持bean的引用. <bean id="bean的 ...

  8. Zabbix-3.0.3结合Grafana-3.1.0给你想要的绘图

    导读 Grafana 是 Graphite 和 InfluxDB 仪表盘和图形编辑器.Grafana 是开源的,功能齐全的度量仪表盘和图形编辑器,支持 Graphite,InfluxDB 和 Open ...

  9. 【BZOJ4002】[JLOI2015]有意义的字符串 数学

    [BZOJ4002][JLOI2015]有意义的字符串 Description B 君有两个好朋友,他们叫宁宁和冉冉.有一天,冉冉遇到了一个有趣的题目:输入 b;d;n,求 Input 一行三个整数 ...

  10. Android FragmentActivity 给Fragment传值

    1.Fragment给Activity传值 定义一个在fragment 中 定义interface 监听器,让activity实现,并将activity的引用传递给fragment.即setListe ...