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

相关推荐
Zane199439 分钟前
暴力字符串匹配失配就要把文本指针往回拉,KMP凭什么能让它一直往前走
算法
Tim_101 小时前
【LeetCode】338、比特位计数
c++·算法·leetcode
木子算法1 小时前
不是重心也不是中点:费马—韦伯点、它的最优性条件与迭代求解
人工智能·算法·目标跟踪
tryxr2 小时前
矩阵的几种基础变换
java·数据结构·算法·矩阵
mmmmath_33 小时前
LeetCode.028.找出字符串中第一个匹配项的
数据结构·算法·leetcode
Selvaggia3 小时前
DMD(Distribution Matching Distillation,分布匹配蒸馏)
算法
Navigator_Z3 小时前
LeetCode //C - 1255. Maximum Score Words Formed by Letters
c语言·算法·leetcode
All for pursuit.3 小时前
【栈-4】739.每日温度
数据结构·c++·算法·leetcode
All for pursuit.3 小时前
【栈-5】84.柱状图中最大的矩形
数据结构·c++·算法·leetcode
wzdark4 小时前
数据压缩算法的时间复杂度与压缩率权衡4
算法