std::find, std::find_if, std::find_if_not
| ヘッダー <algorithm> で定義 |
||
| (1) | ||
template< class InputIt, class T > InputIt find( InputIt first, InputIt last, const T& value ); |
(C++20 以降 constexpr) (C++26まで) |
|
| template< class InputIt, class T = typename std::iterator_traits <InputIt>::value_type > |
(C++26以降) | |
| (2) | ||
| template< class ExecutionPolicy, class ForwardIt, class T > ForwardIt find( ExecutionPolicy&& policy, |
(C++17以降) (C++26まで) |
|
| template< class ExecutionPolicy, class ForwardIt, class T = typename std::iterator_traits |
(C++26以降) | |
template< class InputIt, class UnaryPred > InputIt find_if( InputIt first, InputIt last, UnaryPred p ); |
(3) | (C++20 以降 constexpr) |
| template< class ExecutionPolicy, class ForwardIt, class UnaryPred > ForwardIt find_if( ExecutionPolicy&& policy, |
(4) | (C++17以降) |
template< class InputIt, class UnaryPred > InputIt find_if_not( InputIt first, InputIt last, UnaryPred q ); |
(5) | (C++11以降) (C++20 以降 constexpr) |
| template< class ExecutionPolicy, class ForwardIt, class UnaryPred > ForwardIt find_if_not( ExecutionPolicy&& policy, |
(6) | (C++17以降) |
指定された条件を満たす範囲 [first, last) 内の最初の要素へのイテレータを返します (そのようなイテレータがない場合は last を返します)。
find は value と等しい要素を検索します (operator== を使用)。find_if は述語 p が true を返す要素を検索します。find_if_not は述語 q が false を返す要素を検索します。|
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である。 |
(C++20まで) |
|
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> は true です。 |
(C++20以降) |
目次 |
[編集] パラメータ
| first, last | - | 検査する要素の範囲を定義するイテレータのペア |
| value | - | 要素と比較する値 |
| policy | - | 使用する 実行ポリシー |
| p | - | 必要な要素に対して true を返す単項述語。 式 p(v) は、値カテゴリに関係なく、 |
| q | - | 必要な要素に対して false を返す単項述語。 式 q(v) は、値カテゴリに関係なく、 |
| 型要件 | ||
-InputIt は LegacyInputIterator の要件を満たす必要があります。 | ||
-ForwardIt は LegacyForwardIterator の要件を満たさなければなりません。 | ||
-UnaryPredicate は Predicate の要件を満たさなければなりません。 | ||
[編集] 戻り値
以下の条件を満たす 範囲 [first, last) 内の最初のイテレータ it。そのようなイテレータがない場合は last。
[編集] 計算量
std::distance(first, last) を N とする
operator== を使用して value との比較を最大 N 回行います。[編集] 例外
ExecutionPolicy というテンプレートパラメータを持つオーバーロードは、次のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外をスローし、
ExecutionPolicyが 標準ポリシー のいずれかである場合、std::terminate が呼び出されます。その他のExecutionPolicyの場合、動作は実装定義です。 - アルゴリズムがメモリの割り当てに失敗した場合、std::bad_alloc がスローされます。
[編集] 可能な実装
| find (1) |
|---|
template<class InputIt, class T = typename std::iterator_traits<InputIt>::value_type> constexpr InputIt find(InputIt first, InputIt last, const T& value) { for (; first != last; ++first) if (*first == value) return first; return last; } |
| find_if (3) |
template<class InputIt, class UnaryPred> constexpr InputIt find_if(InputIt first, InputIt last, UnaryPred p) { for (; first != last; ++first) if (p(*first)) return first; return last; } |
| find_if_not (5) |
template<class InputIt, class UnaryPred> constexpr InputIt find_if_not(InputIt first, InputIt last, UnaryPred q) { for (; first != last; ++first) if (!q(*first)) return first; return last; } |
[編集] 注釈
C++11 が利用できない場合、std::find_if_not と同等の機能は、否定された述語を持つ std::find_if を使用することです。
template<class InputIt, class UnaryPred> InputIt find_if_not(InputIt first, InputIt last, UnaryPred q) { return std::find_if(first, last, std::not1(q)); } |
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__cpp_lib_algorithm_default_value_type |
202403 |
(C++26) | アルゴリズム (1,2) のためのリスト初期化 |
[編集] 例
次の例は、与えられたシーケンス内の数値を検索します。
#include <algorithm> #include <array> #include <cassert> #include <complex> #include <initializer_list> #include <iostream> #include <vector> bool is_even(int i) { return i % 2 == 0; } void example_contains() { const auto haystack = {1, 2, 3, 4}; for (const int needle : {3, 5}) if (std::find(haystack.begin(), haystack.end(), needle) == haystack.end()) std::cout << "haystack does not contain " << needle << '\n'; else std::cout << "haystack contains " << needle << '\n'; } void example_predicate() { for (const auto& haystack : {std::array{3, 1, 4}, {1, 3, 5}}) { const auto it = std::find_if(haystack.begin(), haystack.end(), is_even); if (it != haystack.end()) std::cout << "haystack contains an even number " << *it << '\n'; else std::cout << "haystack does not contain even numbers\n"; } } void example_list_init() { std::vector<std::complex<double>> haystack{{4.0, 2.0}}; #ifdef __cpp_lib_algorithm_default_value_type // T gets deduced making list-initialization possible const auto it = std::find(haystack.begin(), haystack.end(), {4.0, 2.0}); #else const auto it = std::find(haystack.begin(), haystack.end(), std::complex{4.0, 2.0}); #endif assert(it == haystack.begin()); } int main() { example_contains(); example_predicate(); example_list_init(); }
出力
haystack contains 3 haystack does not contain 5 haystack contains an even number 4 haystack does not contain even numbers
[編集] 欠陥レポート
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 283 | C++98 | T は EqualityComparable であることが求められていましたが、InputIt の値型は T ではない可能性があります |
要件が削除されました |
[編集] 関連項目
| 等しい(または指定された述語を満たす)最初の2つの隣接する項目を見つける (関数テンプレート) | |
| 特定の範囲内で最後の要素のシーケンスを見つける (関数テンプレート) | |
| 要素の集合のうちいずれか1つを検索する (関数テンプレート) | |
| 2つの範囲が異なる最初の位置を見つける (関数テンプレート) | |
| 要素の範囲の最初の出現を検索する (関数テンプレート) | |
| (C++20)(C++20)(C++20) |
特定の基準を満たす最初の要素を見つける (アルゴリズム関数オブジェクト) |