std:: binary_search
|
定义于头文件
<algorithm>
|
||
| (1) | ||
|
template
<
class
ForwardIt,
class
T
>
bool
binary_search
(
ForwardIt first, ForwardIt last,
|
(自 C++20 起为 constexpr)
(直至 C++26) |
|
|
template
<
class
ForwardIt,
class
T
=
typename
std::
iterator_traits
<
ForwardIt
>
::
value_type
>
|
(自 C++26 起) | |
| (2) | ||
|
template
<
class
ForwardIt,
class
T,
class
Compare
>
bool
binary_search
(
ForwardIt first, ForwardIt last,
|
(自 C++20 起为 constexpr)
(直至 C++26) |
|
|
template
<
class
ForwardIt,
class
T
=
typename
std::
iterator_traits
<
ForwardIt
>
::
value_type
,
|
(自 C++26 起) | |
检查在已分区范围
[
first
,
last
)
内是否出现与
value
等效的元素。
|
若在
若满足以下任意条件,则行为未定义:
|
(C++20 前) |
|
等价于 std :: binary_search ( first, last, value, std:: less { } ) 。 |
(C++20 起) |
[
first
,
last
)
范围内存在某个迭代器
iter
使得
!
bool
(
comp
(
*
iter, value
)
)
&&
!
bool
(
comp
(
value,
*
iter
)
)
为
true
,则返回
true
。否则返回
false
。
-
对于范围
[first,last)中的任何元素 elem , bool ( comp ( elem, value ) ) 不蕴含 ! bool ( comp ( value, elem ) ) 。 -
范围
[first,last)中的元素 elem 未针对表达式 bool ( comp ( elem, value ) ) 和 ! bool ( comp ( value, elem ) ) 进行 分区 。
目录 |
参数
| first, last | - | 定义待检验元素分区 范围 的迭代器对 |
| value | - | 用于与元素比较的值 |
| comp | - |
二元谓词,若第一参数排序于第二参数之前则返回
true
。
谓词函数的签名应等价于如下形式: bool pred ( const Type1 & a, const Type2 & b ) ;
虽然签名不必包含
const
&
,但函数不得修改传递的对象,且必须能接受所有(可能为 const 的)
|
| 类型要求 | ||
-
ForwardIt
必须满足
LegacyForwardIterator
的要求。
|
||
-
Compare
必须满足
BinaryPredicate
的要求,但不必满足
Compare
的要求。
|
||
返回值
当找到与 value 等价的元素时返回 true ,否则返回 false 。
复杂度
给定 N 为 std:: distance ( first, last ) :
然而,若
ForwardIt
不满足
LegacyRandomAccessIterator
要求,则迭代器递增次数与
N
呈线性关系。
注释
尽管
std::binary_search
仅要求
[
first
,
last
)
区间被划分,但该算法通常用于
[
first
,
last
)
已排序的情况,如此二分搜索对任意
value
均有效。
std::binary_search
仅检查是否存在等价元素。若要获取指向该元素的迭代器(如果存在),应改用
std::lower_bound
。
| 功能测试 宏 | 值 | 标准 | 功能 |
|---|---|---|---|
__cpp_lib_algorithm_default_value_type
|
202403
|
(C++26) | 列表初始化 用于算法 ( 1,2 ) |
可能的实现
| binary_search (1) |
|---|
template<class ForwardIt, class T = typename std::iterator_traits<ForwardIt>::value_type> bool binary_search(ForwardIt first, ForwardIt last, const T& value) { return std::binary_search(first, last, value, std::less{}); } |
| binary_search (2) |
template<class ForwardIt, class T = typename std::iterator_traits<ForwardIt>::value_type, class Compare> bool binary_search(ForwardIt first, ForwardIt last, const T& value, Compare comp) { first = std::lower_bound(first, last, value, comp); return (!(first == last) and !(comp(value, *first))); } |
示例
#include <algorithm> #include <cassert> #include <complex> #include <iostream> #include <vector> int main() { const auto haystack = {1, 3, 4, 5, 9}; for (const auto needle : {1, 2, 3}) { std::cout << "正在搜索 " << needle << '\n'; if (std::binary_search(haystack.begin(), haystack.end(), needle)) std::cout << "已找到 " << needle << '\n'; else 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::binary_search(nums.cbegin(), nums.cend(), {4, 2}, cmpz)); #else assert(std::binary_search(nums.cbegin(), nums.cend(), CD{4, 2}, cmpz)); #endif }
输出:
正在搜索 1 已找到 1 正在搜索 2 未找到! 正在搜索 3 已找到 3
缺陷报告
下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
| 缺陷报告 | 应用于 | 发布时行为 | 正确行为 |
|---|---|---|---|
| LWG 270 | C++98 |
Compare
需要满足
Compare
且
T
需要
是 LessThanComparable (要求严格弱序) |
仅需满足划分要求;
允许异构比较 |
| LWG 787 | C++98 | 最多允许 log 2 (N)+2 次比较 | 修正为 log 2 (N)+O(1) |
参见
|
返回匹配特定键的元素范围
(函数模板) |
|
|
返回指向第一个
不小于
给定值的元素的迭代器
(函数模板) |
|
|
返回指向第一个
大于
某值的元素的迭代器
(函数模板) |
|
|
(C++20)
|
确定元素是否存在于部分有序范围内
(算法函数对象) |