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();

相关推荐
非优秀程序员5 小时前
第05章 播放器大战,暴风、快播与那一夜|《互联网产品恩仇录》
算法
武子康12 小时前
驾驶轨迹换个坐标,就能给移动机器人用吗?
人工智能·算法·机器学习
lisw0518 小时前
Dayhoff的PAM替换矩阵理论评介!
线性代数·算法·矩阵
一起学安全技术18 小时前
拆大模型 03|注意力的一句话版本:不用论文讲清 Attention
深度学习·算法·语言模型·transformer
Delightrs20 小时前
Rust LeetCode238 算法题解
开发语言·算法·rust
自信1504130575920 小时前
重生之从0开始学习c++之——vector-中篇
c++·学习·算法
GoAI1 天前
DeepSeek Harness Memory (一)主流记忆插件总结
后端·算法·面试
Pniubi1 天前
力扣54.螺旋矩阵
算法·leetcode·矩阵