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();
相关推荐
刘卜卜&嵌入式33 分钟前
C++_设计模式_观察者模式(Observer Pattern)
c++·观察者模式·设计模式
h汉堡1 小时前
C++入门基础
开发语言·c++·学习
XINVRY-FPGA1 小时前
XCZU7EG‑L1FFVC1156I 赛灵思XilinxFPGA ZynqUltraScale+ MPSoC EG
c++·嵌入式硬件·阿里云·fpga开发·云计算·fpga·pcb工艺
HtwHUAT1 小时前
实验四 Java图形界面与事件处理
开发语言·前端·python
鄃鳕1 小时前
QSS【QT】
开发语言·qt
Tech Synapse1 小时前
基于Surprise和Flask构建个性化电影推荐系统:从算法到全栈实现
python·算法·flask·协同过滤算法
汤姆_5111 小时前
【c语言】深度理解指针4——sizeof和strlen
c语言·开发语言
終不似少年遊*2 小时前
国产之光DeepSeek架构理解与应用分析04
人工智能·python·深度学习·算法·大模型·ds
天天扭码2 小时前
一分钟解决 | 高频面试算法题——最大子数组之和
前端·算法·面试
碎梦归途2 小时前
23种设计模式-结构型模式之外观模式(Java版本)
java·开发语言·jvm·设计模式·intellij-idea·外观模式