std::upper_bound() 算法
- 自 C++20 起
- 直到 C++20
// (1)
template< class ForwardIt, class T >
constexpr ForwardIt upper_bound( ForwardIt first, ForwardIt last, const T& value );
// (2)
template< class ForwardIt, class T, class Compare >
constexpr ForwardIt upper_bound( ForwardIt first, ForwardIt last, const T& value, Compare comp );
// (1)
template< class ForwardIt, class T >
ForwardIt upper_bound( ForwardIt first, ForwardIt last, const T& value );
// (2)
template< class ForwardIt, class T, class Compare >
ForwardIt upper_bound( ForwardIt first, ForwardIt last, const T& value, Compare comp );
返回一个迭代器,指向范围 [first
; last
) 中第一个满足 value < element
(或 comp(value, element)
) 为 true
的元素(即严格大于 value
的元素),如果没有找到这样的元素,则返回 last
。
范围 [first
; last
) 必须根据表达式 !(value < element)
或 !comp(value, element)
进行分区,即,所有表达式为 true
的元素必须出现在所有表达式为 false
的元素之前。
完全排序的范围满足此条件。
- (1) 使用
operator<
比较元素。 - (2) 使用给定的比较函数
comp
。
参数
first last | 要检查的部分有序范围。 |
值 | 用于比较元素的 `value`。 |
comp | 比较函数对象(即满足 Compare 要求的对象)。比较函数的签名应等同于以下内容:
|
类型要求
ForwardIt | LegacyForwardIterator |
Compare | BinaryPredicate |
`Compare` 不要求满足 Compare。
返回值
迭代器,指向范围 [first
; last
) 中第一个满足 value < element
(或 comp(value, element)
) 为 true
的元素,如果没有找到这样的元素,则返回 last
。
复杂度
然而,对于非 LegacyRandomAccessIterators,迭代器增量次数是线性的。
值得注意的是,std::map
、std::multimap
、std::set
和 std::multiset
的迭代器不是随机访问迭代器,因此应优先使用它们的成员 upper_bound()
函数。
异常
(无)
可能的实现
upper_bound (1)
template<class ForwardIt, class T>
ForwardIt upper_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 (!(value < *it))
{
first = ++it;
count -= step + 1;
}
else
count = step;
}
return first;
}
upper_bound (2)
template<class ForwardIt, class T, class Compare>
ForwardIt upper_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(value, *it))
{
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 < 7; ++i)
{
// Search first element that is greater than i
auto upper = std::upper_bound(data.begin(), data.end(), i);
std::cout << i << " < ";
upper != data.end()
? std::cout << *upper << " at index " << std::distance(data.begin(), upper)
: 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::upper_bound(prices.begin(), prices.end(), to_find,
[](double value, const PriceInfo& info)
{
return value < info.price;
});
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 < 2 at index 1
2 < 4 at index 2
3 < 4 at index 2
4 < 5 at index 3
5 < 6 at index 5
6 < not found
107.3 at index 4
110.2 not found