std::lower_bound
定义于头文件 <algorithm> |
||
(1) | ||
template< class ForwardIt, class T > ForwardIt lower_bound( ForwardIt first, ForwardIt last, |
(constexpr since C++20)(constexpr 自 C++20 起) (until C++26)(C++26 前) |
|
template< class ForwardIt, class T = typename std::iterator_traits <ForwardIt>::value_type > |
(since C++26)(自 C++26 起) | |
(2) | ||
template< class ForwardIt, class T, class Compare > ForwardIt lower_bound( ForwardIt first, ForwardIt last, |
(constexpr since C++20)(constexpr 自 C++20 起) (until C++26)(C++26 前) |
|
template< class ForwardIt, class T = typename std::iterator_traits <ForwardIt>::value_type, |
(since C++26)(自 C++26 起) | |
在已划分范围 [
first,
last)
中搜索不先于 value 排序的首个元素。
返回范围 若 |
(until C++20)(C++20 前) |
等价于 std::lower_bound(first, last, value, std::less{})。 |
(since C++20)(自 C++20 起) |
[
first,
last)
中首个令 bool(comp(*iter, value)) 为 false 的迭代器 iter,或若不存在这种 iter 则返回 last。内容 |
[编辑] 参数
first, last | - | 定义要检查的已划分元素范围的迭代器对 |
value | - | 与元素比较的值 |
comp | - | 若首个参数先于第二个参数排序则返回 true 的二元谓词。 谓词函数的签名应等价于以下形式: bool pred(const Type1 &a, const Type2 &b); 虽然签名不需要有 const &,函数仍必须不修改传递给它的对象,且必须能接受类型 |
类型要求 | ||
-ForwardIt 必须满足 LegacyForwardIterator 的要求。 | ||
-Compare 必须满足 BinaryPredicate 的要求。不需要满足 Compare。 |
[编辑] 返回值
指向范围 [
first,
last)
中不先于 value 排序的首个元素的迭代器,或若找不到这种元素则为 last。
[编辑] 复杂度
设 N 为 std::distance(first, last)
然而,若 ForwardIt
不是 LegacyRandomAccessIterator,则迭代器自增数与 N 成线性关系。注意,std::map、std::multimap、std::set 和 std::multiset 迭代器不是随机访问迭代器,故应优先选用它们的成员 lower_bound
函数。
[编辑] 可能的实现
lower_bound (1) |
---|
template<class ForwardIt, class T = typename std::iterator_traits<ForwardIt>::value_type> ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T& value) { return std::lower_bound(first, last, value, std::less{}); } |
lower_bound (2) |
template<class ForwardIt, class T = typename std::iterator_traits<ForwardIt>::value_type, class Compare> ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T& value, Compare comp) { ForwardIt it; typename std::iterator_traits<ForwardIt>::difference_type count, step; count = std::distance(first, last); while (count > 0) { it = first; step = count / 2; std::advance(it, step); if (comp(*it, value)) { first = ++it; count -= step + 1; } else count = step; } return first; } |
[编辑] 注解
虽然 std::lower_bound
仅要求 [
first,
last)
为已划分,此算法通常用于 [
first,
last)
已排序的情况,从而二分搜索对任何 value 皆有效。
与 std::binary_search 相反,std::lower_bound
不要求 operator< 或 comp 为非对称(即 a < b 与 b < a 始终有异果)。实际上,甚至不要求对 [
first,
last)
中的任何迭代器 iter,value < *iter 或 comp(value, *iter) 为良构。
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_algorithm_default_value_type |
202403 |
(C++26) | 列表初始化 用于算法 (1,2) |
[编辑] 示例
#include <algorithm> #include <cassert> #include <complex> #include <iostream> #include <vector> struct PriceInfo { double price; }; int main() { const std::vector<int> data{1, 2, 4, 5, 5, 6}; for (int i = 0; i < 8; ++i) { // Search for first element x such that i ≤ x auto lower = std::lower_bound(data.begin(), data.end(), i); std::cout << i << " ≤ "; lower != data.end() ? std::cout << *lower << " at index " << std::distance(data.begin(), lower) : std::cout << "not found"; std::cout << '\n'; } std::vector<PriceInfo> prices{{100.0}, {101.5}, {102.5}, {102.5}, {107.3}}; for (const double to_find : {102.5, 110.2}) { auto prc_info = std::lower_bound(prices.begin(), prices.end(), to_find, [](const PriceInfo& info, double value) { return info.price < value; }); prc_info != prices.end() ? std::cout << prc_info->price << " at index " << prc_info - prices.begin() : std::cout << to_find << " not found"; std::cout << '\n'; } using CD = std::complex<double>; std::vector<CD> nums{{1, 0}, {2, 2}, {2, 1}, {3, 0}}; auto cmpz = [](CD x, CD y) { return x.real() < y.real(); }; #ifdef __cpp_lib_algorithm_default_value_type auto it = std::lower_bound(nums.cbegin(), nums.cend(), {2, 0}, cmpz); #else auto it = std::lower_bound(nums.cbegin(), nums.cend(), CD{2, 0}, cmpz); #endif assert((*it == CD{2, 2})); }
输出
0 ≤ 1 at index 0 1 ≤ 1 at index 0 2 ≤ 2 at index 1 3 ≤ 4 at index 2 4 ≤ 4 at index 2 5 ≤ 5 at index 3 6 ≤ 6 at index 5 7 ≤ not found 102.5 at index 2 110.2 not found
[编辑] 缺陷报告
下列行为更改缺陷报告被追溯性地应用于先前发布的 C++ 标准。
DR | 应用于 | 发布时的行为 | 正确行为 |
---|---|---|---|
LWG 270 | C++98 | Compare 被要求满足 Compare 且 T 被要求为 LessThanComparable (要求严格弱序) |
仅要求划分; 容许异质比较 |
LWG 384 | C++98 | 至多容许 log(N)+1 次比较 | 更正为 log2(N)+1 |
LWG 2150 | C++98 | 若在 [ first, last) 中存在任何迭代器 iter 使得bool(comp(*iter, value)) 为 false,则 std::lower_bound 可能返回 [ iter, last) 中的任何迭代器 |
不得返回 iter 后的迭代器 iter 后不得返回迭代器 |
[编辑] 参见
返回匹配特定键的元素范围 (函数模板) | |
将元素范围划分为两个组 (函数模板) | |
(C++11) |
定位已划分范围的划分点 (函数模板) |
返回指向首个大于特定值的元素的迭代器 (函数模板) | |
返回指向首个不小于给定键的元素的迭代器 ( std::set<Key,Compare,Allocator> 的公共成员函数) | |
返回指向首个不小于给定键的元素的迭代器 ( std::multiset<Key,Compare,Allocator> 的公共成员函数) | |
(C++20) |
返回指向首个不小于给定值的元素的迭代器 (算法函数对象) |