std::lower_bound() 算法
- 自 C++20 起
- 直到 C++20
// (1)
template< class ForwardIt, class T >
constexpr ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value );
// (2)
template< class ForwardIt, class T, class Compare >
constexpr ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value, Compare comp );
// (1)
template< class ForwardIt, class T >
ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value );
// (2)
template< class ForwardIt, class T, class Compare >
ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value, Compare comp );
返回一个迭代器,指向范围 [first
; last
) 中第一个使得 element < value
(或 comp(element, value)
)为 false
的元素(即大于或等于 value 的元素),如果未找到此类元素,则返回 last
。
范围 [first
; last
) 必须根据表达式 element < value
(或 comp(element, value)
)进行分区,即所有使表达式为 true
的元素必须位于所有使表达式为 false
的元素之前。
完全排序的范围满足此条件。
- (1) 使用
operator<
比较元素。 - (2) 使用给定的比较函数
comp
。
参数
first last | 要检查的部分有序范围。 |
值 | 用于比较元素的 `value`。 |
comp | 比较函数对象(即满足 Compare 要求的对象)。比较函数的签名应等同于以下内容:
|
类型要求
ForwardIt | LegacyForwardIterator |
Compare | BinaryPredicate |
`Compare` 不要求满足 Compare。
返回值
迭代器,指向范围 [first
; last
) 中第一个使得 element < value
(或 comp(element, value)
)为 false
的元素,如果未找到此类元素,则返回 last
。
复杂度
执行的比较次数与 first
和 last
之间的距离呈对数关系(最多 log^2(last - first) + O(1)
次比较)。
然而,对于非 LegacyRandomAccessIterators,迭代器增量次数是线性的。
值得注意的是,std::map
、std::multimap
、std::set
和 std::multiset
迭代器不是随机访问迭代器,因此应优先使用它们的成员 lower_bound()
函数。
异常
(无)
可能的实现
lower_bound (1)
template<class ForwardIt, class T>
ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T& value)
{
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 (*it < value)
{
first = ++it;
count -= step + 1;
}
else
count = step;
}
return first;
}
lower_bound (2)
template<class ForwardIt, class T, 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;
}
示例
#include <algorithm>
#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 (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';
}
}
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