std::ranges::binary_search
来自cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| 在标头 <algorithm> 定义
|
||
| 调用签名 |
||
| (1) | ||
template< std::forward_iterator I, std::sentinel_for<I> S, class T, class Proj = std::identity, std::indirect_strict_weak_order <const T*, std::projected<I, Proj>> Comp = ranges::less > constexpr bool binary_search( I first, S last, const T& value, Comp comp = {}, Proj proj = {} ); |
(C++20 起) (C++26 前) |
|
template< std::forward_iterator I, std::sentinel_for<I> S, class Proj = std::identity, class T = std::projected_value_t<I, Proj>, std::indirect_strict_weak_order <const T*, std::projected<I, Proj>> Comp = ranges::less > constexpr bool binary_search( I first, S last, const T& value, Comp comp = {}, Proj proj = {} ); |
(C++26 起) | |
| (2) | ||
template< ranges::forward_range R, class T, class Proj = std::identity, std::indirect_strict_weak_order <const T*, std::projected<ranges::iterator_t<R>, Proj>> Comp = ranges::less > constexpr bool binary_search( R&& r, const T& value, Comp comp = {}, Proj proj = {} ); |
(C++20 起) (C++26 前) |
|
template< ranges::forward_range R, class Proj = std::identity, class T = std::projected_value_t<ranges::iterator_t<R>, Proj>, std::indirect_strict_weak_order <const T*, std::projected<ranges::iterator_t<R>, Proj>> Comp = ranges::less > constexpr bool binary_search( R&& r, const T& value, Comp comp = {}, Proj proj = {} ); |
(C++26 起) | |
1) 检查范围
[first, last) 内是否出现等价于 value 的经投影元素。2) 同 (1),但以
r 为源范围,如同以 ranges::begin(r) 为 first 并以 ranges::end(r) 为 last。欲成功进行 ranges::binary_search,范围 [first, last) 必须至少以相对 value 部分有序,即它必须满足下列要求:
- 相对于
std::invoke(comp, std::invoke(proj, element), value)已划分(即该表达式对其为true的所有元素处于该表达式对其为false的所有元素之前)。 - 相对于
!std::invoke(comp, value, std::invoke(proj, element))已划分。 - 对于所有元素,若
std::invoke(comp, std::invoke(proj, element), value)为true则!std::invoke(comp, value, std::invoke(proj, element))为true。
完全有序的范围符合这些判别标准。
此页面上描述的函数式实体是算法函数对象(非正式地称为 niebloid),即:
参数
| first, last | - | 要检验的元素范围的迭代器-哨位对 |
| r | - | 要检验的元素范围 |
| value | - | 与元素比较的值 |
| comp | - | 应用到经投影元素的比较函数 |
| proj | - | 应用到元素的投影 |
返回值
若找到等于 value 的元素则为 true,否则为 false 。
复杂度
进行的比较与投影次数与 first 和 last 间的距离成对数(至多 log2(last - first) + O(1) 次投影与比较)。然而,对于不实现 std::random_access_iterator 的迭代器,迭代器自增次数为线性。
注解
std::ranges::binary_search 在找到投影等于 value 的元素时并不会返回指向所找到元素的迭代器。如果需要迭代器,应当代之以使用 std::ranges::lower_bound。
| 功能特性测试宏 | 值 | 标准 | 功能特性 |
|---|---|---|---|
__cpp_lib_algorithm_default_value_type |
202403 |
(C++26) | 算法中的列表初始化 (1,2) |
可能的实现
struct binary_search_fn
{
template<std::forward_iterator I, std::sentinel_for<I> S,
class Proj = std::identity, class T = std::projected_value_t<I, Proj>,
std::indirect_strict_weak_order
<const T*, std::projected<I, Proj>> Comp = ranges::less>
constexpr bool operator()(I first, S last, const T& value,
Comp comp = {}, Proj proj = {}) const
{
auto x = ranges::lower_bound(first, last, value, comp, proj);
return (!(x == last) && !(std::invoke(comp, value, std::invoke(proj, *x))));
}
template<ranges::forward_range R, class Proj = std::identity,
class T = std::projected_value_t<ranges::iterator_t<R>, Proj>,
std::indirect_strict_weak_order
<const T*, std::projected<ranges::iterator_t<R>,
Proj>> Comp = ranges::less>
constexpr bool operator()(R&& r, const T& value, Comp comp = {}, Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r), value,
std::move(comp), std::move(proj));
}
};
inline constexpr binary_search_fn binary_search;
|
示例
运行此代码
#include <algorithm>
#include <cassert>
#include <complex>
#include <iostream>
#include <ranges>
#include <vector>
int main()
{
constexpr static auto haystack = {1, 3, 4, 5, 9};
static_assert(std::ranges::is_sorted(haystack));
for (const int needle : std::views::iota(1)
| std::views::take(3))
{
std::cout << "寻找 " << needle << ": ";
std::ranges::binary_search(haystack, needle)
? std::cout << "找到了 " << needle << '\n'
: std::cout << "未命中!\n";
}
using CD = std::complex<double>;
std::vector<CD> nums{{1, 1}, {2, 3}, {4, 2}, {4, 3}};
auto cmpz = [](CD x, CD y){ return abs(x) < abs(y); };
#ifdef __cpp_lib_algorithm_default_value_type
assert(std::ranges::binary_search(nums, {4, 2}, cmpz));
#else
assert(std::ranges::binary_search(nums, CD{4, 2}, cmpz));
#endif
}
输出:
寻找 for 1: 找到了 1
寻找 for 2: 未命中!
寻找 for 3: 找到了 3
参阅
(C++20) |
返回匹配特定键值的元素范围 (算法函数对象) |
(C++20) |
返回首个不小于 给定值的元素的迭代器 (算法函数对象) |
(C++20) |
返回首个大于 给定值的元素的迭代器 (算法函数对象) |
(C++23)(C++23) |
检查范围是否包含给定元素或子范围 (算法函数对象) |
| 判断元素是否在偏序范围中 (函数模板) |