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();
相关推荐
拼好饭和她皆失9 分钟前
dfs 第一次加训 详解 下
算法·深度优先
梁下轻语的秋缘2 小时前
每日c/c++题 备战蓝桥杯(P2241 统计方形(数据加强版))
c语言·c++·蓝桥杯
学习编程的gas4 小时前
C++面向对象编程入门:从类与对象说起(一)
开发语言·c++
冼紫菜4 小时前
【Spring Boot 多模块项目】@MapperScan失效、MapperScannerConfigurer 报错终极解决方案
java·开发语言·mybatis
Bear on Toilet5 小时前
Bug日记——实现“日期类”
开发语言·c++·bug
apcipot_rain5 小时前
《面向对象程序设计-C++》实验五 虚函数的使用及抽象类
开发语言·c++
明月看潮生7 小时前
青少年编程与数学 02-019 Rust 编程基础 05课题、复合数据类型
开发语言·青少年编程·rust·编程与数学
幼稚诠释青春7 小时前
Java学习笔记(对象)
java·开发语言
zhczzm7 小时前
深入浅出之STL源码分析2_stl与标准库,编译器的关系
c++