当前位置:网站首页>图解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在有序序列中,查找更加高效。
边栏推荐
- [1154]如何将字符串转换为datetime
- cudaMemcpy study notes
- CMOS和TTL的区别?
- The Sad History of Image Processing Technology
- Arbitrum 专访 | L2 Summer, 脱颖而出的 Arbitrum 为开发者带来了什么?
- leetcode-399: division evaluation
- 加密生活,Web3 项目合伙人的一天
- CV-Model【3】:MobileNet v2
- Introduction to flask series 】 【 flask - using SQLAlchemy
- ShardingJDBC usage summary
猜你喜欢

Drools基本介绍,入门案例,基本语法

Classic linked list OJ strong training problem - fast and slow double pointer efficient solution

leetcode-1161: Maximum in-layer element sum

uniapp uses 3rd party fonts

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

ShardingJDBC使用总结

基于FPGA的售货机

Software testing basic interface testing - getting started with Jmeter, you should pay attention to these things

Force buckled brush the stairs (7/30)

The effective square of the test (one question of the day 7/29)
随机推荐
Clustering index, and what is the difference between a clustering index
汉诺塔问题
力扣刷题之有效的正方形(每日一题7/29)
12 pictures take you to fully understand service current limit, circuit breaker, downgrade, and avalanche
Arbitrum Interview | L2 Summer, what does the standout Arbitrum bring to developers?
AtCoder Beginner Contest 261 Partial Solution
Arbitrum 专访 | L2 Summer, 脱颖而出的 Arbitrum 为开发者带来了什么?
Validate XML documents
Observer mode (1)
19.支持向量机-优化目标和大间距直观理解
The real CTO is a technical person who understands products
【Bank Series Phase 1】People's Bank of China
Mathematical Ideas in AI
How to do a startup CTO?
vlan间路由+静态路由+NAT(PAT+静态NAT)综合实验
验证整数输入
拒绝加班,程序员开发的效率工具集
coldfusion8 background scheduled tasks take shell
Hanyuan Hi-Tech 8-channel HDMI integrated multi-service high-definition video optical transceiver 8-channel HDMI video + 8-channel two-way audio + 8-channel 485 data + 8-channel E1 + 32-channel teleph
Classic linked list OJ strong training problem - fast and slow double pointer efficient solution