lower_bound()函数,upper_bound()函数
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()函数的更多相关文章
- STL之std::set、std::map的lower_bound和upper_bound函数使用说明
由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊 ...
- C++ lower_bound 与 upper_bound 函数
头文件: #include <algorithm> 二分查找的函数有 3 个: 参考:C++ lower_bound 和upper_bound lower_bound(起始地址,结束地址 ...
- lower_bound和upper_bound函数
lower_bound(ForwardIter first,ForwardIter last,const_TP & val) upper_bound(ForwardIter first,For ...
- Maximum Value(unique函数,lower_bound()函数,upper_bound()函数的使用)
传送门 在看大佬的代码时候遇到了unique函数以及二分查找的lower_bound和upper_bound函数,所以写这篇文章来记录以备复习. unique函数 在STL中unique函数是一个去重 ...
- C++二分查找:lower_bound( )和upper_bound( )
#include<algorithm>//头文件 //标准形式 lower_bound(int* first,int* last,val); upper_bound(int* first, ...
- C++中lower_bound函数和upper_bound函数
STL中关于二分查找的函数有三个lower_bound .upper_bound .binary_search .这三个函数都运用于有序区间(当然这也是运用二分查找的前提),下面记录一下这两个函数. ...
- 二分检索函数lower_bound()和upper_bound()
二分检索函数lower_bound()和upper_bound() 一.说明 头文件:<algorithm> 二分检索函数lower_bound()和upper_bound() lower ...
- 奇思妙想:利用野指针和lower_bound()/upper_bound()函数实现整数二分
众所周知,c++的STL中提供了三个二分查找函数,binary_search(),lower_bound(),upper_bound(),功能分别是找某值是否在数组中出现,找到数组中第一个大于等于某值 ...
- STL之lower_bound和upper_bound
ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...
随机推荐
- Xcode升级后插件失败解决方法
大家都知道每次升级Xcode 然后插件都不能用了,最根本的原因是每一个插件都有个Info.plist文件,这个文件里有个key-DVTPlugInCompatibilityUUIDs记录了能够使用该插 ...
- HttpModule的简单示例
1.HttpModule可用在asp.net 管线事件触发的过程中.. 可处理一些通用的操作,如给特定请求加 gzip压缩. 2.示例代码: using System; using System.We ...
- MongoDB基本用法
MongoDB基本用法(增删改高级查询.mapreduce) 分享一下我经常用到的自己写的mongo用法示例 该示例基于当前最新的mongo驱动,版本为mongo-2.10.1.jar,用junit写 ...
- jQuery制作简洁的多级联动Select下拉框
今天我们要来分享一款很实用的jQuery插件,它是一个基于jQuery多级联动的省市地区Select下拉框,并且值得一提的是,这款联动下拉框是经过自定义美化过的,外观比浏览器自带的要漂亮许多.另外,这 ...
- UESTC 1511(差分约束)
题目链接:http://acm.uestc.edu.cn/problem.php?pid=1511 思路:我们可以等到这样的5个关系式: k=1:dsit[a]-dist[b]>=0&& ...
- Redis分布式锁,基于StringRedisTemplate和基于Lettuce实现setNx
使用redis分布式锁,来确保多个服务对共享数据操作的唯一性一般来说有StringRedisTemplate和RedisTemplate两种redis操作模板. 根据key-value的类型决定使用哪 ...
- Laravel5.1 搭建博客 --上传文件及文件管理
教程源自:Laravel学院 这一节 咱来说说上传文件的功能实现,我们会把上传的文件保存到项目本地,不仅上传 还有删除和预览功能. 1 配置 我们先从配置开始做起,先修改我们自己创建的 blog.ph ...
- 在静态工具类中需要注入mapper
在xml中 <bean id="messageUtil" class="org.ldd.ssm.hangyu.utils.MessageUtil" ini ...
- 如何使用 awk 复合表达式
导读 一直以来在查对条件是否匹配时,我们使用的都是简单的表达式.那如果你想用超过一个表达式来查对特定的条件呢?本文中,我们将看看如何在过滤文本和字符串时,结合多个表达式,即复合表达式,用以查对条件. ...
- [Android] 开源框架 xUtils HttpUtils 代理设置 (Temporary Redirect错误)
今天简单学习了一下xUtils的使用 https://github.com/wyouflf/xUtils 其中用到HttpUtils模块时,发现总是出现Temporary Redirect 错误. 查 ...