std::ranges::binary_search
定義於標頭檔案 <algorithm> |
||
呼叫簽名 (Call signature) |
||
(1) | ||
template< std::forward_iterator I, std::sentinel_for<I> S, class T, class Proj = std::identity, |
(C++20 起) (直到 C++26) |
|
template< std::forward_iterator I, std::sentinel_for<I> S, class Proj = std::identity, |
(C++26 起) | |
(2) | ||
template< ranges::forward_range R, class T, class Proj = std::identity, |
(C++20 起) (直到 C++26) |
|
template< ranges::forward_range R, class Proj = std::identity, |
(C++26 起) | |
[
first,
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。
完全排序的範圍滿足這些條件。
本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即
- 呼叫它們中的任何一個時,不能指定顯式模板引數列表。
- 它們中的任何一個都對 引數依賴查詢 不可見。
- 當它們中的任何一個透過 normal unqualified lookup 作為函式呼叫運算子左側的名稱找到時,argument-dependent lookup 將被抑制。
目錄 |
[編輯] 引數
first, last | - | 定義要檢查的元素 範圍 的迭代器-哨兵對 |
r | - | 要檢查的元素範圍 |
value | - | 用於比較元素的數值 |
comp | - | 應用於投影元素的比較函式 |
proj | - | 應用於元素的投影 |
[編輯] 返回值
如果找到一個與 value 相等的元素,則為 true,否則為 false。
[編輯] 複雜度
執行的比較和投影次數與 first 和 last 之間的距離呈對數關係(最多 log2(last - first) + O(1) 次比較和投影)。然而,對於不符合 std::random_access_iterator 模型的迭代器-哨兵對,迭代器增量次數是線性的。
[編輯] 注意
當找到一個投影等於 value 的元素時,std::ranges::binary_search
不會返回指向該元素的迭代器。如果需要迭代器,應使用 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 << "Searching for " << needle << ": "; std::ranges::binary_search(haystack, needle) ? std::cout << "found " << needle << '\n' : std::cout << "no dice!\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 }
輸出
Searching for 1: found 1 Searching for 2: no dice! Searching for 3: found 3
[編輯] 另請參閱
(C++20) |
返回與特定鍵匹配的元素範圍 (演算法函式物件) |
(C++20) |
返回一個指向第一個不小於給定值的元素的迭代器 (演算法函式物件) |
(C++20) |
返回一個指向第一個大於某個值的元素的迭代器 (演算法函式物件) |
(C++23)(C++23) |
檢查範圍是否包含給定的元素或子範圍 (演算法函式物件) |
判斷一個元素是否存在於部分有序的範圍中 (函式模板) |