std::ranges::binary_search
| ヘッダー <algorithm> で定義 |
||
| 呼び出しシグネチャ |
||
| (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 です。
完全にソートされた範囲はこれらの基準を満たします。
このページで説明されている関数のようなエンティティは、アルゴリズム関数オブジェクト(非公式にはニーブロイドとして知られている)です。つまり、
- これらのいずれかを呼び出す際に、明示的なテンプレート引数リストを指定することはできません。
- これらのいずれも実引数依存の名前探索には見えません。
- これらのいずれかが関数呼び出し演算子の左側の名前として通常の非修飾名探索によって見つかった場合、実引数依存の名前探索は抑制されます。
目次 |
[編集] Parameters
| first, last | - | 調査する要素の範囲を定義するイテレータとセンチネルのペア |
| r | - | 調査する要素の範囲 |
| value | - | 要素と比較する値 |
| comp | - | 射影された要素に適用する比較関数 |
| proj | - | 要素に適用する射影 |
[編集] Return value
true value と等しい要素が見つかった場合、それ以外の場合は false。
[編集] Complexity
first と last の間の距離の対数(最大 log2(last - first) + O(1) 回の比較および投影)で比較と投影の回数が実行されます。ただし、イテレータ-センチネルペアが std::random_access_iterator をモデル化しない場合、イテレータのインクリメント回数は線形になります。
[編集] Notes
std::ranges::binary_search は、投影された要素が value と等しい要素が見つかった場合でも、その要素へのイテレータを返しません。イテレータが必要な場合は、代わりに std::ranges::lower_bound を使用してください。
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__cpp_lib_algorithm_default_value_type |
202403 |
(C++26) | アルゴリズム (1,2) のためのリスト初期化 |
[編集] Possible implementation
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; |
[編集] Example
#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
[編集] See also
| (C++20) |
特定のキーに一致する要素の範囲を返す (アルゴリズム関数オブジェクト) |
| (C++20) |
与えられた値より小さくない最初の要素へのイテレータを返す (アルゴリズム関数オブジェクト) |
| (C++20) |
特定の値より大きい最初の要素へのイテレータを返す (アルゴリズム関数オブジェクト) |
| (C++23)(C++23) |
範囲が指定された要素または部分範囲を含むかチェックする (アルゴリズム関数オブジェクト) |
| 部分的に順序付けられた範囲に要素が存在するかどうかを判断する (関数テンプレート) |