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
の要件を満たさなければならない。
|
||
戻り値
範囲
range
[
first
,
last
)
内で以下の条件を満たす最初のイテレータ
it
を返す。そのようなイテレータが存在しない場合は
last
を返す:
計算量
与えられた N を std:: distance ( first, last ) として:
operator==
を使用して行う。
例外
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++コードは完全に保持されています - C++固有の用語(`template`, `class`, `constexpr`, `InputIt`, `UnaryPred`など)は翻訳されていません - 関数名(`find`, `find_if`, `find_if_not`)はそのまま保持されています - コード内のコメントや説明文がないため、翻訳対象の自然言語テキストはありませんでした
注記
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つの隣接する要素を見つける
(関数テンプレート) |
|
|
特定の範囲内での要素シーケンスの最後の出現を見つける
(関数テンプレート) |
|
|
要素の集合のいずれかを検索する
(関数テンプレート) |
|
|
2つの範囲が最初に異なる位置を見つける
(関数テンプレート) |
|
|
要素の範囲の最初の出現を検索する
(関数テンプレート) |
|
|
(C++20)
(C++20)
(C++20)
|
特定の基準を満たす最初の要素を見つける
(アルゴリズム関数オブジェクト) |