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();
相关推荐
YuTaoShao3 分钟前
【LeetCode 热题 100】56. 合并区间——排序+遍历
java·算法·leetcode·职场和发展
Code Warrior3 分钟前
【每日算法】专题五_位运算
开发语言·c++
沐知全栈开发2 小时前
HTML DOM 访问
开发语言
脑袋大大的3 小时前
JavaScript 性能优化实战:减少 DOM 操作引发的重排与重绘
开发语言·javascript·性能优化
二进制person4 小时前
Java SE--方法的使用
java·开发语言·算法
OneQ6665 小时前
C++讲解---创建日期类
开发语言·c++·算法
JoJo_Way5 小时前
LeetCode三数之和-js题解
javascript·算法·leetcode
码农不惑5 小时前
2025.06.27-14.44 C语言开发:Onvif(二)
c语言·开发语言
.30-06Springfield5 小时前
人工智能概念之七:集成学习思想(Bagging、Boosting、Stacking)
人工智能·算法·机器学习·集成学习
Coding小公仔7 小时前
C++ bitset 模板类
开发语言·c++