C++二分函数lower_bound和upper_bound的用法

两者都是定义在头文件里。用二分搜索在一个有序数组中使用特定规则进行查找特定元素,时间复杂度就是O(logN)。

基础用法

在升序数组中查找特定元素。

cpp 复制代码
vector<int> arr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int x = 6;

//lower_bound 基础用法
//查找arr中第一个大于等于x的数,返回它的索引指针
auto it = lower_bound(arr.begin(), arr.end(), x);
//查找arr中第一个大于等于x的数,返回它的下标
int pos = lower_bound(arr.begin(), arr.end(), x) - arr.begin();

//upper_bound 基础用法
//查找arr中第一个大于x的数,返回它的索引指针
auto it = upper_bound(arr.begin(), arr.end(), x);
//查找arr中第一个大于x的数,返回它的下标
int pos = upper_bound(arr.begin(), arr.end(), x) - arr.begin();

用greater()重载

在降序数组中查找特定元素。

cpp 复制代码
vector<int> arr = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
int x = 6;

//lower_bound 用greater<type>()重载
//查找arr中第一个小于等于x的数,返回它的索引指针
auto it = lower_bound(arr.begin(), arr.end(), x, greater<int>());
//查找arr中第一个小于等于x的数,返回它的下标
int pos = lower_bound(arr.begin(), arr.end(), x, greater<int>()) - arr.begin();

//upper_bound 用greater<type>()重载
//查找arr中第一个小于x的数,返回它的索引指针
auto it = upper_bound(arr.begin(), arr.end(), x, greater<int>());
//查找arr中第一个小于x的数,返回它的下标
int pos = upper_bound(arr.begin(), arr.end(), x, greater<int>()) - arr.begin();

复合数组自定义重载函数

cpp 复制代码
//arr 是一个pair数组,按照pair的first元素从小到大排序,pair的first元素相同时按照pair的second元素从小到大排序
vector<pair<int, int>> arr = {{0, 1}, {0, 3}, {2, 1}, {2, 6}, {3, 3}, {4, 2}, {5, 1}};

//因为是按照pair的first元素排的序,所以自定义函数时只能使用pair的first进行比较
//lower_bound 自定义重载函数,函数接收两个参数,第一个表示数组中的元素(这里是pair元素),第二个表示要查找的值(也是lower_bound函数中的x)
//查找arr中第一个first大于等于x的pair元素,返回它的索引指针
auto it = lower_bound(arr.begin(), arr.end(), x, [](const pair<int, int>& p, const int b){return p.first < b;});
//查找arr中第一个first大于x的pair元素,返回它的索引指针
auto it = lower_bound(arr.begin(), arr.end(), x, [](const pair<int, int>& p, const int b){return p.first <= b;});
//查找arr中第一个first大于等于x的pair元素,返回它的下标
int pos = lower_bound(arr.begin(), arr.end(), x, [](const pair<int, int>& p, const int b){return p.first < b;}) - arr.begin();
//查找arr中第一个first大于x的pair元素,返回它的下标
int pos = lower_bound(arr.begin(), arr.end(), x, [](const pair<int, int>& p, const int b){return p.first <= b;}) - arr.begin();

//upper_bound 自定义重载函数,函数接收两个参数,第一个表示要查找的值(也是upper_bound函数中的x),第二个表示数组中的元素(这里是pair元素)
//查找arr中第一个大于等于x的数,返回它的索引指针
auto it = upper_bound(arr.begin(), arr.end(), x, [](const int b, const pair<int, int>& p){return b <= p.first;});
//查找arr中第一个大于x的数,返回它的索引指针
auto it = upper_bound(arr.begin(), arr.end(), x, [](const int b, const pair<int, int>& p){return b < p.first;});
//查找arr中第一个大于x的数,返回它的下标
int pos = upper_bound(arr.begin(), arr.end(), x, [](const int b, const pair<int, int>& p){return b <= p.first;}) - arr.begin();
//查找arr中第一个大于等于x的数,返回它的下标
int pos = upper_bound(arr.begin(), arr.end(), x, [](const int b, const pair<int, int>& p){return b < p.first;}) - arr.begin();
相关推荐
小道士写程序4 分钟前
Qt 5.12 上读取 .xlsx 文件(Windows 平台)
开发语言·windows·qt
ou.cs16 分钟前
c# :this() 和 :base()区别
开发语言·c#
闪电麦坤9520 分钟前
数据结构:泰勒展开式:霍纳法则(Horner‘s Rule)
数据结构·算法
凌佚26 分钟前
rknn优化教程(一)
c++·目标检测·性能优化
Mikhail_G35 分钟前
Python应用函数调用(二)
大数据·运维·开发语言·python·数据分析
木木黄木木2 小时前
Python制作史莱姆桌面宠物!可爱的
开发语言·python·宠物
exploration-earth2 小时前
本地优先的状态管理与工具选型策略
开发语言·前端·javascript
suuijbd2 小时前
个人总结八股文之-基础篇(持续更新)
算法
2401_881244402 小时前
斐波那契数列------矩阵幂法
线性代数·算法·矩阵
苦学编程的谢3 小时前
Java网络编程API 1
java·开发语言·网络