关于lower_bound()和upper_bound()
关于lower_bound()
和upper_bound()
:
参考:关于lower_bound( )和upper_bound( )的常见用法
注意:查找的数组必须要是排好序的。因为,它们查找的方式也是二分查找,所以,复杂度为log(n)
①从小到大排序
lower_bound(begin,end,num)
:从数组的begin
位置到end-1
位置二分查找第一个大于或等于num
的数字,找到并返回该数字的地址,不存在则返回end
。通过返回的地址减去起始地址begin
,得到找到数字在数组中的下标。
upper_bound(begin,end,num)
:从数组的begin
位置到end-1
位置二分查找第一个大于num
的数字,找到并返回该数字的地址,不存在则返回end
。通过返回的地址减去起始地址begin
,得到找到数字在数组中的下标。
②从大到小排序
lower_bound(begin,end,num,greater<type>())
:从数组的begin
位置到end-1
位置二分查找第一个小于或等于num
的数字,找到并返回该数字的地址,不存在则返回end
。通过返回的地址减去起始地址begin
,得到找到数字在数组中的下标。
upper_bound(begin,end,num,greater<type>())
:从数组的begin
位置到end-1
位置二分查找第一个小于num
的数字,找到并返回该数字的地址,不存在则返回end
。通过返回的地址减去起始地址begin
,得到找到数字在数组中的下标。
总结:如果是lower_bound()
会有等于,upper_bound()
没有等于,默认是大于(或等于),加了greater<type>()
是小于(或等于)
关于lower_bound()和upper_bound()的更多相关文章
- STL源码学习----lower_bound和upper_bound算法
转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...
- [STL] lower_bound和upper_bound
STL中的每个算法都非常精妙, ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一 ...
- vector的插入、lower_bound、upper_bound、equal_range实例
对于这几个函数的一些实例以便于理解: #include <cstdlib> #include <cstdio> #include <cstring> #includ ...
- STL中的lower_bound和upper_bound的理解
STL迭代器表述范围的时候,习惯用[a, b),所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置. 譬如,值val在容器内的时候,从 ...
- STL 源码分析《5》---- lower_bound and upper_bound 详解
在 STL 库中,关于二分搜索实现了4个函数. bool binary_search (ForwardIterator beg, ForwardIterator end, const T& v ...
- lower_bound和upper_bound算法
参考:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html ForwardIter lower_bound(ForwardIte ...
- lower_bound 和 upper_bound
Return iterator to lower bound Returns an iterator pointing to the first element in the range [first ...
- STL源码学习----lower_bound和upper_bound算法[转]
STL中的每个算法都非常精妙,接下来的几天我想集中学习一下STL中的算法. ForwardIter lower_bound(ForwardIter first, ForwardIter last,co ...
- [转] STL源码学习----lower_bound和upper_bound算法
http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html PS: lower_bound of value 就是最后一个 < ...
- STL algorithm算法lower_bound和upper_bound(31)
lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...
随机推荐
- springboot指定redis库编号配置实现
yml配置 spring: redis: database: #shiro host: 127.0.0.1 port: timeout: password: null redis-cache: dat ...
- spark学习之Lambda架构日志分析流水线
单机运行 一.环境准备 Flume 1.6.0 Hadoop 2.6.0 Spark 1.6.0 Java version 1.8.0_73 Kafka 2.11-0.9.0.1 zookeeper ...
- 安装sshpass
sshpass: 用于非交互的ssh 密码验证 ssh登陆不能在命令行中指定密码,也不能以shell中随处可见的,sshpass 的出现,解决了这一问题.它允许你用 -p 参数指定明文密码,然后直接 ...
- 【Git的基本操作一】文件初始化及设置签名
1. 本地库初始化 命令: git init 效果:
- Flink接收RabbitMQ数据写入到Oracle
文件内容 项目案例: https://github.com/TaoPanfeng/case/tree/master/03-flink/flink-rabbitmq-oracle FlinkMain.j ...
- 红队基础设施建设:隐藏你的C2
0x01 前言 待定
- JavaJDBC【六、连接池】
背景 1.连接是一种重要资源 2.频繁连接数据库会增加数据库压力 常用连接池 dbcp 1.导入jar包(官网下载) commons-dbcp.jar commons-pool.jar commons ...
- kali工具的总结
由于篇幅有限,只列举部分,ps:第一次发有什么不对的 还望各位大大指正 nc 瑞士军刀 [v1.10-41] 使用格式: nc [-参数] 主机名 端口[s] [端口] … 侦听入站: nc -l - ...
- 【wifi移植 3】开发板wifi自动获取IP
内核版本:3.4.61 1. 配置内核,支持DHCP ~/kernel$ make menuconfig [*] Networking support ---> Networking opti ...
- win32 控件的使用
我们建立的项目都是基于对话框的win32项目,和主窗口一样对话框也是窗口的一种类型所以区别不是很大,所以我们再下面讲一下控件的使用(里面不要使用char,要开始使用WCHAR,他的很多函数都是wcs. ...