当前位置:网站首页>图解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在有序序列中,查找更加高效。
边栏推荐
- Drools基本介绍,入门案例,基本语法
- Drools basic introduction, introductory case, basic syntax
- mysql 索引
- 【银行系列第一期】中国人民银行
- Intel's software and hardware optimization empowers Neusoft to accelerate the arrival of the era of smart medical care
- The application of AI in the whole process of medical imaging equipment
- Maximum area of solar panel od js
- Brute Force/Adjacency Matrix Breadth First Directed Weighted Graph Undirected Weighted Graph
- Go 项目实战-获取多级分类下的全部商品
- LeetCode 1161 The largest element in the layer and the LeetCode road of [BFS binary tree] HERODING
猜你喜欢
AI在医疗影像设备全流程应用
Drools基本介绍,入门案例,基本语法
1. Non-type template parameters 2. Specialization of templates 3. Explanation of inheritance
Face detection based on opencv
General introduction to the Unity interface
Fiddler captures packets to simulate weak network environment testing
曼城推出可检测情绪的智能围巾,把球迷给整迷惑了
真正的CTO,是一个懂产品的技术人
ShardingJDBC基本介绍
What does a software test report contain?
随机推荐
coldfusion8 background scheduled tasks take shell
The modification is not properly placed in the sandbox, causing Apple compatibility issues
Verify the integer input
What level of software testing does it take to get a 9K job?
LeetCode 1161 The largest element in the layer and the LeetCode road of [BFS binary tree] HERODING
【银行系列第一期】中国人民银行
系统需求多变如何设计
Drools基本介绍,入门案例,基本语法
Draw Your Cards
multiplayer-hlap 包有问题,无法升级的解决方案
Go 项目实战-获取多级分类下的全部商品
f.grid_sample
拒绝加班,程序员开发的效率工具集
vlan间路由+静态路由+NAT(PAT+静态NAT)综合实验
如何在 go 程序中暴露 Prometheus 指标
Between two orderly array of additive and Topk problem
Detailed explanation of STP election (step + case)
StringJoiner详解
MPPT solar charge controller data collection - through the gateway acquisition capacity battery SOC battery voltage, wi-fi
Introduction and use of Drools WorkBench