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未満) | |
template< class InputIt, class T > constexpr InputIt find( InputIt first, InputIt last, const T& value ); |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt, class T > ForwardIt find( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, const T& value ); |
(2) | (C++17以上) |
(3) | ||
template< class InputIt, class UnaryPredicate > InputIt find_if( InputIt first, InputIt last, |
(C++20未満) | |
template< class InputIt, class UnaryPredicate > constexpr InputIt find_if( InputIt first, InputIt last, |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate > ForwardIt find_if( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, |
(4) | (C++17以上) |
(5) | ||
template< class InputIt, class UnaryPredicate > InputIt find_if_not( InputIt first, InputIt last, |
(C++11以上) (C++20未満) |
|
template< class InputIt, class UnaryPredicate > constexpr InputIt find_if_not( InputIt first, InputIt last, |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate > ForwardIt find_if_not( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, |
(6) | (C++17以上) |
範囲 [first, last)
内の特定の基準を満たす最初の要素を返します。
find
は value
と等しい要素を検索します。find_if
は述語 p
が true を返す要素を検索します。find_if_not
は述語 q
が false を返す要素を検索します。policy
に従って実行されます。 このオーバーロードは、std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である場合にのみ、オーバーロード解決に参加します目次 |
[編集] 引数
first, last | - | 調べる要素の範囲 |
value | - | 要素と比較する値 |
policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
p | - | 要求された要素に対して true を返す単項述語。 式 p(v) は |
q | - | 要求された要素に対して false を返す単項述語。 式 q(v) は |
型の要件 | ||
-InputIt は LegacyInputIterator の要件を満たさなければなりません。
| ||
-ForwardIt は LegacyForwardIterator の要件を満たさなければなりません。
| ||
-UnaryPredicate は Predicate の要件を満たさなければなりません。
|
[編集] 戻り値
条件を満たす最初の要素を指すイテレータ、またはそのような要素が見つからない場合は last
。
[編集] 計算量
多くとも last
- first
回の述語の適用。
[編集] 例外
テンプレート引数 ExecutionPolicy
を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicy
が標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆるExecutionPolicy
については、動作は処理系定義です。 - アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。
[編集] 実装例
1つめのバージョン |
---|
template<class InputIt, class T> constexpr InputIt find(InputIt first, InputIt last, const T& value) { for (; first != last; ++first) { if (*first == value) { return first; } } return last; } |
2つめのバージョン |
template<class InputIt, class UnaryPredicate> constexpr InputIt find_if(InputIt first, InputIt last, UnaryPredicate p) { for (; first != last; ++first) { if (p(*first)) { return first; } } return last; } |
3つめのバージョン |
template<class InputIt, class UnaryPredicate> constexpr InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate 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 UnaryPredicate> InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q) { return std::find_if(first, last, std::not1(q)); } |
[編集] 例
以下の例は整数のベクタから整数を探します。
#include <iostream> #include <algorithm> #include <vector> #include <iterator> int main() { int n1 = 3; int n2 = 5; std::vector<int> v{0, 1, 2, 3, 4}; auto result1 = std::find(std::begin(v), std::end(v), n1); auto result2 = std::find(std::begin(v), std::end(v), n2); if (result1 != std::end(v)) { std::cout << "v contains: " << n1 << '\n'; } else { std::cout << "v does not contain: " << n1 << '\n'; } if (result2 != std::end(v)) { std::cout << "v contains: " << n2 << '\n'; } else { std::cout << "v does not contain: " << n2 << '\n'; } }
出力:
v contains: 3 v does not contain: 5
[編集] 関連項目
同じ要素 (または��定された述語を満たす要素) 2つが隣接している最初の位置を探します (関数テンプレート) | |
指定された要素の並びが現れる最後の位置を探します (関数テンプレート) | |
指定された要素のいずれかが現れる位置を探します (関数テンプレート) | |
2つの範囲が異なる最初の位置を探します (関数テンプレート) | |
指定範囲の要素に対して検索を行います (関数テンプレート) |