std::binary_search
受约束算法及范围上的算法 (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
受约束算法: std::ranges::copy, std::ranges::sort, ... | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
执行策略 (C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
不修改序列的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
修改序列的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Partitioning operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
划分操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
排序操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
二分搜索操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
集合操作(在已排序范围上) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
堆操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
最小/最大操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
排列 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
数值运算 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
未初始化存储上的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
C 库 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
在标头
<algorithm>
定义
|
||
(1) | ||
template< class ForwardIt, class T >
bool binary_search( ForwardIt first, ForwardIt last, const T& value ); |
(C++20 前) | |
template< class ForwardIt, class T >
constexpr bool binary_search( ForwardIt first, ForwardIt last, const T& value ); |
(C++20 起) | |
(2) | ||
template< class ForwardIt, class T, class Compare >
bool binary_search(
ForwardIt first, ForwardIt last, |
(C++20 前) | |
template< class ForwardIt, class T, class Compare >
constexpr bool
binary_search( ForwardIt first, ForwardIt
last, |
(C++20 起) | |
检查等价于 value 的元素是否出现在范围
[
first,
last)
中出现。
std::binary_search
要成功,范围 [
first,
last)
就必须至少相对于
value 部分有序,即它必须满足下列所有要求:
- 已相对 element < value 或 comp(element, value) 划分
- 已相对 !(value < element) 或 !comp(value, element) 划分(即所有令此表达式为 true 的元素必须在所有令此表达式为 false 的元素之前)
- 对于所有元素,如果若 element < value 或 comp(element, value) 是 true,那么 !(value < element) 或 !comp(value, element) 也是 true
已经完全排序的范围满足这些判别标准。
第一版本用 operator< 比较元素,第二版本用给定的比较函数 comp。
参数
first, last | - | 要检验的元素范围 |
value | - | 要与元素比较的值 |
comp | - | 如果第一参数小于(即先序于)第二个则返回 true
的二元谓词。 谓词函数的签名应等价于如下: bool pred(const Type1 &a, const Type2 &b); 虽然签名不必有 const &
,函数也不能修改传递给它的对象,而且必须接受(可为 const 的)类型 |
类型要求 | ||
-ForwardIt 必须符合老式向前迭代器 (LegacyForwardIterator)
的要求。
|
||
-Compare 必须符合二元谓词 (BinaryPredicate)
的要求。不要求满足 比较 (Compare)
。
|
返回值
找到等于 value 的元素时返回 true,否则返回 false。
复杂度
进行的比较次数与 first 和 last 间的距离成对数(至多 log
2(last
- first) + O(1) 次比较)。然而,对于非老式随机访问迭代器
(LegacyRandomAccessIterator)
,迭代器自增次数呈线性。
可能的实现
版本一 |
---|
template<class ForwardIt, class T> bool binary_search(ForwardIt first, ForwardIt last, const T& value) { first = std::lower_bound(first, last, value); return (!(first == last) && !(value < *first)); } |
版本二 |
template<class ForwardIt, class T, 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) && !(comp(value, *first))); } |
示例
#include <iostream> #include <algorithm> #include <vector> int main() { std::vector<int> haystack{1, 3, 4, 5, 9}; std::vector<int> needles{1, 2, 3}; for (auto needle : needles) { std::cout << "正在搜索 " << needle << '\n'; if (std::binary_search(haystack.begin(), haystack.end(), needle)) std::cout << "找到了 " << needle << '\n'; else std::cout << "没找到!\n"; } }
输出:
正在搜索 1 找到了 1 正在搜索 2 没找到! 正在搜索 3 找到了 3
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
LWG 270 | C++98 | Compare 需要满足比较 (Compare)
,并且 T 需要是可小于比较 (LessThanComparable) 的(要求严格弱序) |
只需要划分;容许异相比较 |
LWG 787 | C++98 | 最多允许 log(last - first) + 2 次比较 | 改成 log 2(last - first) + O(1) 次 |
参阅
返回匹配特定键值的元素范围 (函数模板) |