lower_bound()以及upper_bound()

lower_bound():

lower_bound()的返回值是第一个大于等于 target 的值的地址,用这个地址减去first,得到的就是第一个大于等于target的值的下标。

在数组中:

int pos=lower_bound(a,a+n,target)-a;\\n为数组长度

vector容器中:

int pos=lower_bound(a.begin(),a.end(),target)-a.begin();

upper_bound():

从小到大 的排好序的数组中,在数组的 [begin, end) 区间中二分查找第一个大于value的数,找到返回该数字的地址,没找到则返回end。用这个地址减去first,得到的就是第一个大于等于target的值的下标。

在数组中:

int pos=upper_bound(a,a+n,target)-a;\\n为数组长度

vector容器中:

int pos=upper_bound(a.begin(),a.end(),target)-a.begin();

相关推荐
u01092727120 小时前
C++中的策略模式变体
开发语言·c++·算法
2501_9418372620 小时前
停车场车辆检测与识别系统-YOLOv26算法改进与应用分析
算法·yolo
六义义1 天前
java基础十二
java·数据结构·算法
四维碎片1 天前
QSettings + INI 笔记
笔记·qt·算法
Tansmjs1 天前
C++与GPU计算(CUDA)
开发语言·c++·算法
独自破碎E1 天前
【优先级队列】主持人调度(二)
算法
weixin_445476681 天前
leetCode每日一题——边反转的最小成本
算法·leetcode·职场和发展
打工的小王1 天前
LeetCode Hot100(一)二分查找
算法·leetcode·职场和发展
Swift社区1 天前
LeetCode 385 迷你语法分析器
算法·leetcode·职场和发展
sonadorje1 天前
svd在图像处理中的应用
算法