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 起) | |
检查与 value 等价的元素是否出现在已划分的范围 [
first,
last)
中。
如果在 如果满足以下任何条件,则行为是未定义的:
|
(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)) 进行 划分。
目录 |
[edit] 参数
first, last | - | 定义要检查的元素已分区范围的迭代器对。 |
value | - | 用于比较元素的数值 |
comp | - | 二元谓词,如果第一个参数排在第二个参数之前,则返回true。 谓词函数的签名应等效于以下内容: bool pred(const Type1 &a, const Type2 &b); 尽管签名不需要包含 const &,但函数不得修改传递给它的对象,并且必须能够接受类型为 (可能为 const) |
类型要求 | ||
-ForwardIt 必须满足 LegacyForwardIterator 的要求。 | ||
-Compare 必须满足 BinaryPredicate 的要求。不要求它满足 Compare。 |
[edit] 返回值
如果找到与 value 等价的元素,则返回 true,否则返回 false。
[edit] 复杂度
给定 N 为 std::distance(first, last)
但是,如果 ForwardIt
不是 LegacyRandomAccessIterator,则迭代器增量的次数与 N 成线性关系。
[edit] 注意
尽管 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) |
[edit] 可能的实现
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))); } |
[edit] 示例
#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 << "Searching for " << needle << '\n'; if (std::binary_search(haystack.begin(), haystack.end(), needle)) std::cout << "Found " << needle << '\n'; else std::cout << "Not found!\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 }
输出
Searching for 1 Found 1 Searching for 2 Not found! Searching for 3 Found 3
[edit] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 270 | C++98 | Compare 需要满足 比较,且 T 需要是 可小于比较的(需要严格弱序) |
只需要分区; 允许异构比较 |
LWG 787 | C++98 | 最多允许 log2(N)+2 次比较 | 更正为 log2(N)+O(1) |
[edit] 另请参阅
返回与特定键匹配的元素范围 (函数模板) | |
返回一个指向第一个不小于给定值的元素的迭代器 (函数模板) | |
返回一个指向第一个大于某个值的元素的迭代器 (函数模板) | |
(C++20) |
判断一个元素是否存在于部分有序的范围中 (算法函数对象) |