当前位置:网站首页>Graphical lower_bound & upper_bound
Graphical lower_bound & upper_bound
2022-07-31 02:48:00 【Taoxi Xiaosheng】
Official explanation of the two algorithms
template< class ForwardIt, class T >ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value );
lower_bound is used to find the position of the first not less thanvalue in the ascending sequence, that is, to find the first>=values position.
template< class ForwardIt, class T >ForwardIt upper_bound( ForwardIt first, ForwardIt last, const T& value );
upper_bound is used to find the first position in the ascending sequence that is greater than value.
Take it literally, with a little twist, let's look at two examples.
Element exists in list
For sequence 1 2 2 3 3 4 4 5 5 6 6 7
We make lower_bound and upper_bound calls for value=3.
Because lower_bound requires the first >=, the position marked by [ in the figure is the target position
Because upper_bound requires the first>, the position marked with ) in the figure is the target position.
- Element does not exist in list
For sequence 1 3 3 6 6 9 9
We make lower_bound and upper_bound calls for value=4.
Because lower_bound requires the first >=, the position marked by [ in the figure is the target position
Because upper_bound requires the first>, the position marked with ) in the figure is the target position.
It can be seen from this that if the return value of lower_bound is equal to value, it means that value exists in the list, otherwise it does not exist.This is also a way to find an element in a list.
In this way, the names of lower_bound and upper_bound are easy to understand, "lower boundary and upper boundary", the lower boundary is a closed interval, and the upper boundary is an open interval, forming a left-closed and right-open interval [ )
p>This design is consistent with the left-closed and right-open principle that STL iterators follow. All begin includes the target element, so the end does not include the target element.
We know that there is also a commonly used search function find in stl, which can be used to find elements. What is the difference between the two functions?
ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value );InputIt find( InputIt first, InputIt last, const T& value );
Similarities: Both accept head and tail iterators and target elements.
Differences:
- find does not require elements to be in order, lower_bound requires order.
- find uses == to compare elements, and lower_bound uses < to compare elements.This is why lower_bound requires elements to be sorted.
- find is linear search, lower_bound is binary search.
It can be seen that find is more general, while lower_bound is more efficient to search in ordered sequences.
边栏推荐
- coldfusion8 background scheduled tasks take shell
- 如何搭建私有yum源
- 完整复制虚拟机原理(云计算)
- To write good test cases, you must first learn test design
- The modification is not properly placed in the sandbox, causing Apple compatibility issues
- Pythagorean tuple od js
- 经典链表OJ强训题——快慢双指针高效解法
- 【C语言基础】解决C语言error: expected ‘;‘, ‘,‘ or ‘)‘ before ‘&‘ token
- How to do a startup CTO?
- STM32CUBEMX develops GD32F303 (11) ---- ADC scans multiple channels in DMA mode
猜你喜欢
Live Preview | KDD2022 Doctoral Dissertation Award Champion and Runner-up Dialogue
YOLOV5 study notes (2) - environment installation + operation + training
公司官网建站笔记(六):域名进行公安备案并将备案号显示在网页底部
6、显示评论和回复
【Bank Series Phase 1】People's Bank of China
The comprehensive result of the case statement, do you know it?[Verilog Advanced Tutorial]
How to do a startup CTO?
Pythagorean tuple od js
SQL injection Less54 (limited number of SQL injection + union injection)
Moxa NPort device flaw could expose critical infrastructure to devastating attack
随机推荐
11、Redis实现关注、取消关注以及关注和粉丝列表
print task sorting js od huawei
加密公司向盗窃的黑客提供报价:保留一点,把剩下的归还
字体压缩神器font-spider的使用
execsnoop 工具
1. Non-type template parameters 2. Specialization of templates 3. Explanation of inheritance
Huawei od dice js
The modification is not properly placed in the sandbox, causing Apple compatibility issues
华为分布式存储FusionStorage知识点总结【面试篇】
经典链表OJ强训题——快慢双指针高效解法
Discussion on Service Commitment of Class Objects under Multithreading
SQL injection Less54 (limited number of SQL injection + union injection)
开题报告之论文框架
7. List of private messages
7、私信列表
MPPT太阳能充放电控制器数据采集-通过网关采集电池电压容量电量SOC,wifi传输
Validate XML documents
StringJoiner详解
Draw Your Cards
The application of AI in the whole process of medical imaging equipment