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();
相关推荐
try again!4 分钟前
rollup.js 和 webpack
开发语言·javascript·webpack
阿镇吃橙子6 分钟前
一些手写及业务场景处理问题汇总
前端·算法·面试
酱酱哥玩AI10 分钟前
Trae编译器:实现多目标班翠鸟优化算法(IPKO)无人机路径规划仿真(Python版),完整代码
算法
du fei15 分钟前
C# 窗体应用(.FET Framework) 线程操作方法
开发语言·c#
du fei17 分钟前
C#文件操作
开发语言·c#
MPCTHU23 分钟前
二叉树、排序算法与结构图
数据结构·算法·排序算法
m0_5557629026 分钟前
struct 中在c++ 和c中用法区别
java·c语言·c++
亓才孓29 分钟前
[leetcode]树的操作
算法·leetcode·职场和发展
月亮有痕迹诶29 分钟前
【C++】智能指针
开发语言·c++·c++11
搞不懂语言的程序员35 分钟前
装饰器模式详解
开发语言·python·装饰器模式