在C++的标准模板库(STL)中,lower_bound是一个基于二分查找实现的函数,专门用于在有序序列中查找第一个大于等于目标值的元素位置,时间复杂度为O(log n),比线性查找效率更高,是处理有序数据查找的常用工具。

lower_bound的基本用法
lower_bound定义在<algorithm>头文件中,有两种常用的重载形式,分别适用于普通数组和STL容器迭代器范围。
函数参数说明
第一种形式针对迭代器范围:
// 迭代器版本,查找[first, last)范围内第一个大于等于val的元素 ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const T& val);
第二种形式支持自定义比较规则:
// 带自定义比较函数的版本 ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const T& val, Compare comp);
参数含义如下:
first:查找范围的起始迭代器(包含该位置)last:查找范围的结束迭代器(不包含该位置)val:要查找的目标值comp:自定义比较函数,默认使用小于号比较
返回值是指向第一个大于等于val的元素的迭代器,如果序列中所有元素都小于val,则返回last迭代器。
在普通数组中的使用示例
普通数组也是连续存储的有序序列,同样可以使用lower_bound,需要注意数组名可以转换为指针(迭代器)使用。
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int arr[] = {1, 3, 5, 7, 9, 11};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 7;
// 查找arr中第一个大于等于target的元素位置
int* pos = lower_bound(arr, arr + n, target);
if (pos != arr + n) {
// 计算下标位置
int index = pos - arr;
cout << "找到目标值,位置下标为:" << index << endl;
cout << "对应元素值为:" << *pos << endl;
} else {
cout << "未找到大于等于目标值的元素" << endl;
}
return 0;
}
上述代码中,数组arr是升序排列的,查找目标值7,lower_bound会返回指向元素7的指针,计算得到下标为3,输出对应结果。
在STL容器中的使用示例
对于vector、set等STL有序容器,lower_bound的使用更加直观,直接传入容器的迭代器范围即可。
vector容器示例
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
vector<int> vec = {2, 4, 6, 8, 10};
int target = 5;
// 查找vector中第一个大于等于5的元素
auto it = lower_bound(vec.begin(), vec.end(), target);
if (it != vec.end()) {
cout << "第一个大于等于" << target << "的元素是:" << *it << endl;
cout << "元素下标为:" << it - vec.begin() << endl;
} else {
cout << "vector中所有元素都小于" << target << endl;
}
return 0;
}
该示例中vector是升序的,目标值5不在序列中,lower_bound会返回指向元素6的迭代器,下标为2。
set容器示例
set本身是有序的关联容器,也可以直接使用lower_bound,不过set自身也提供了成员函数lower_bound,两者功能一致,成员函数版本效率更高。
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
int main() {
set<int> s = {1, 2, 3, 4, 5};
int target = 3;
// 使用STL算法版本的lower_bound
auto it1 = lower_bound(s.begin(), s.end(), target);
// 使用set成员函数版本的lower_bound
auto it2 = s.lower_bound(target);
if (it1 != s.end()) {
cout << "算法版本找到元素:" << *it1 << endl;
}
if (it2 != s.end()) {
cout << "成员函数版本找到元素:" << *it2 << endl;
}
return 0;
}
lower_bound与upper_bound的区别
很多开发者会混淆lower_bound和upper_bound,两者的核心差异在于查找条件不同:
| 函数名 | 查找条件 | 返回值说明 |
|---|---|---|
| lower_bound | 第一个大于等于目标值的元素 | 找不到则返回last |
| upper_bound | 第一个大于目标值的元素 | 找不到则返回last |
如果要查找目标值在有序序列中的出现范围,可以结合两个函数使用:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
vector<int> vec = {1, 2, 2, 2, 3, 4};
int target = 2;
auto left = lower_bound(vec.begin(), vec.end(), target);
auto right = upper_bound(vec.begin(), vec.end(), target);
if (left < right) {
cout << "元素" << target << "出现的下标范围是:[" << left - vec.begin() << ", " << right - vec.begin() << ")" << endl;
cout << "出现次数为:" << right - left << endl;
} else {
cout << "元素不存在" << endl;
}
return 0;
}
使用注意事项
lower_bound要求输入的序列必须是升序排列的,如果是降序序列,需要传入自定义的比较函数,或者使用greater<int>()作为比较规则。- 返回的迭代器如果等于
last,说明序列中没有大于等于目标值的元素,使用前需要先做判断,避免越界访问。 - 对于自定义类型的数据,需要重载小于号运算符,或者传入自定义的比较函数,保证
lower_bound能正确比较元素大小。
下面是一个降序序列使用lower_bound的示例:
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
int main() {
vector<int> vec = {10, 8, 6, 4, 2};
int target = 5;
// 降序序列需要传入greater比较规则
auto it = lower_bound(vec.begin(), vec.end(), target, greater<int>());
if (it != vec.end()) {
cout << "降序序列中第一个小于等于" << target << "的元素是:" << *it << endl;
}
return 0;
}
lower_bound有序序列二分查找C++修改时间:2026-07-03 09:45:34