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) |
判斷一個元素是否存在於部分有序的範圍中 (演算法函式物件) |