当前位置:网站首页>图解lower_bound&upper_bound
图解lower_bound&upper_bound
2022-07-31 02:25:00 【桃溪小小生】
两个算法的官方解释
template< class ForwardIt, class T >
ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value );lower_bound用于找出升序序列中第一个不小于value的位置, 即找出第一个>=value的位置。
template< class ForwardIt, class T >
ForwardIt upper_bound( ForwardIt first, ForwardIt last, const T& value );upper_bound用于找出升序序列中第一个大于value的位置。
直接从字面理解,稍微有点绕,让我们看两个例子。
元素在列表中存在

对于序列1 2 2 3 3 4 4 5 5 6 6 7
我们对value=3进行lower_bound和upper_bound的调用。
因为lower_bound要求第一个>=,所以图中 [ 标记的位置就是目标位置
因为upper_bound要求第一个>, 所以图中 ) 标记的位置就是目标位置。
- 元素在列表中不存在

对于序列1 3 3 6 6 9 9
我们对value=4进行lower_bound和upper_bound的调用。
因为lower_bound要求第一个>=,所以图中 [ 标记的位置就是目标位置
因为upper_bound要求第一个>, 所以图中 ) 标记的位置就是目标位置。
由此可以看出,如果lower_bound的返回值等于value,说明value在列表中存在,否则就不存在。这也是一种列表中查找元素的方法。
这样一看,lower_bound和upper_bound的起名就很好理解了,“下边界和上边界”,下边界是闭区间,上边界是开区间,组成了左闭右开的区间[ )
这个设计和STL迭代器遵守的左闭右开原则是一致的,所有的begin都包括目标元素,所以的end都不包括目标元素。
我们知道,stl中还有一个常用的查找函数find,都能用于查找元素,两个函数的区别是什么呢?
ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value );
InputIt find( InputIt first, InputIt last, const T& value );相似点:都接受首尾迭代器和目标元素。
不一样的地方:
- find不要求元素有序,lower_bound要求有序。
- find内部采用==比较元素,lower_bound采用<比较元素。这也是为什么lower_bound要求元素必须是排序好的原因。
- find是线性查找,lower_bound是二分查找。
由此可见,find更通用一些,而lower_bound在有序序列中,查找更加高效。
边栏推荐
- BAT can't sell "Medical Cloud": Hospitals flee, mountains stand, and there are rules
- Can an inexperienced college graduate switch to software testing?my real case
- Maximum monthly salary of 20K?The average salary is nearly 10,000... What is the experience of working in a Huawei subsidiary?
- Teach you how to configure Jenkins automated email notifications
- 静态路由解析(最长掩码匹配原则+主备路由)
- Between two orderly array of additive and Topk problem
- f.grid_sample
- mmdetection训练一个模型相关命令
- What does a software test report contain?
- leetcode-952: Calculate max component size by common factor
猜你喜欢

"Cloud native's master, master and vulgar skills" - 2022 National New College Entrance Examination Volume I Composition

Manchester City confuses fans with smart scarf that detects emotions
![[Map and Set] LeetCode & Niu Ke exercise](/img/66/d812a6ad854cb0993c796760042150.png)
[Map and Set] LeetCode & Niu Ke exercise

MPPT solar charge controller data collection - through the gateway acquisition capacity battery SOC battery voltage, wi-fi

基于opencv实现人脸检测

STM32CUBEMX开发GD32F303(11)----ADC在DMA模式下扫描多个通道

12 pictures take you to fully understand service current limit, circuit breaker, downgrade, and avalanche

coldfusion8 background scheduled tasks take shell

Basic learning about Redis related content

Maximum area of solar panel od js
随机推荐
如何在 go 程序中暴露 Prometheus 指标
Tower of Hanoi problem
What have I experienced to become a tester who is harder than development?
完整复制虚拟机原理(云计算)
f.grid_sample
mysql view
Classic linked list OJ strong training problem - fast and slow double pointer efficient solution
PDF split/merge
Linux下redis7的安装,启动与停止
Introduction to flask series 】 【 flask - using SQLAlchemy
基于FPGA的售货机
Layer 2 broadcast storm (cause + judgment + solution)
LeetCode 1161 最大层内元素和[BFS 二叉树] HERODING的LeetCode之路
MPPT太阳能充放电控制器数据采集-通过网关采集电池电压容量电量SOC,wifi传输
Crypto Life, a day in the life of a Web3 project partner
CV-Model【3】:MobileNet v2
Shell script to loop through values in log file to sum and calculate average, max and min
Android's webview cache related knowledge collection
全流程调度——MySQL与Sqoop
[Map and Set] LeetCode & Niu Ke exercise