当前位置:网站首页>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.
边栏推荐
- php 网站的多语言设置(IP地址区分国内国外)
- 【C语言基础】解决C语言error: expected ‘;‘, ‘,‘ or ‘)‘ before ‘&‘ token
- 工程(五)——小目标检测tph-yolov5
- 10 权限介绍
- Coldfusion file read holes (CVE - 2010-2861)
- AtCoder Beginner Contest 261 Partial Solution
- Moxa NPort device flaw could expose critical infrastructure to devastating attack
- Go 项目实战-获取多级分类下的全部商品
- STM32CUBEMX开发GD32F303(11)----ADC在DMA模式下扫描多个通道
- SQL注入 Less46(order by后的注入+rand()布尔盲注)
猜你喜欢

CentOS7下mysql5.7.37的安装【完美方案】

221. Largest Square

7、私信列表

JS 函数 this上下文 运行时点语法 圆括号 数组 IIFE 定时器 延时器 self.备份上下文 call apply

Huawei od dice js

Discourse Custom Header Links

Intranet Infiltration - Privilege Escalation

8. Unified exception handling (controller notifies @ControllerAdvice global configuration class, @ExceptionHandler handles exceptions uniformly)

【Android】Room —— SQLite的替代品

mycat的主从关系 垂直分库 水平分表 以及mycat分片联表查询的配置详解(mysql5.7系列)
随机推荐
12 磁盘相关命令
英特尔软硬优化,赋能东软加速智慧医疗时代到来
基于opencv实现人脸检测
软件积累 -- 截图软件ScreenToGif
Intel's software and hardware optimization empowers Neusoft to accelerate the arrival of the era of smart medical care
MPPT太阳能充放电控制器数据采集-通过网关采集电池电压容量电量SOC,wifi传输
什么是分布式锁?实现分布式锁的三种方式
CentOS7下mysql5.7.37的卸载【完美方案】
图像处理技术的心酸史
编译Hudi
15. Website Statistics
字体压缩神器font-spider的使用
Linux下redis7的安装,启动与停止
coldfusion8 background scheduled tasks take shell
10. Redis implements likes (Set) and obtains the total number of likes
StringJoiner in detail
Difference between CMOS and TTL?
Unity3D Button 鼠标悬浮进入与鼠标悬浮退出按钮事件
CefSharp入门-winform
Word/Excel fixed table size, when filling in the content, the table does not change with the cell content