名前空間
変種
操作

std::find, std::find_if, std::find_if_not

提供: cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
制約付きアルゴリズムと範囲に対するアルゴリズム (C++20)
コンセプトとユーティリティ: std::sortable, std::projected, ...
制約付きアルゴリズム: std::ranges::copy, std::ranges::sort, ...
実行ポリシー (C++17)
非変更シーケンス操作
(C++11)(C++11)(C++11)
(C++17)
findfind_iffind_if_not
(C++11)
変更シーケンス操作
未初期化記憶域の操作
分割操作
ソート操作
二分探索操作
集合操作 (ソート済み範囲用)
ヒープ操作
(C++11)
最小/最大演算
(C++11)
(C++17)

順列
数値演算
C のライブラリ
 
ヘッダ <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,

                 UnaryPredicate p );
(C++20未満)
template<class InputIt, class UnaryPredicate >

constexpr InputIt find_if( InputIt first, InputIt last,

                           UnaryPredicate p );
(C++20以上)
template<class ExecutionPolicy, class ForwardIt, class UnaryPredicate >

ForwardIt find_if( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last,

                 UnaryPredicate p );
(4) (C++17以上)
(5)
template<class InputIt, class UnaryPredicate >

InputIt find_if_not( InputIt first, InputIt last,

                     UnaryPredicate q );
(C++11以上)
(C++20未満)
template<class InputIt, class UnaryPredicate >

constexpr InputIt find_if_not( InputIt first, InputIt last,

                               UnaryPredicate q );
(C++20以上)
template<class ExecutionPolicy, class ForwardIt, class UnaryPredicate >

ForwardIt find_if_not( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last,

                     UnaryPredicate q );
(6) (C++17以上)

範囲 [first, last) 内の特定の基準を満たす最初の要素を返します。

1)findvalue と等しい要素を検索します。
3)find_if は述語 ptrue を返す要素を検索します。
5)find_if_not は述語 qfalse を返す要素を検索します。
2,4,6)(1,3,5) と同じですが、 policy に従って実行されます。 このオーバーロードは、std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である場合にのみ、オーバーロード解決に参加します

目次

[編集]引数

first, last - 調べる要素の範囲
value - 要素と比較する値
policy - 使用する実行ポリシー。 詳細は実行ポリシーを参照してください
p - 要求された要素に対して ​true を返す単項述語。

p(v)VT 型 (およびその const 修飾された型) のすべての引数 v について、その値カテゴリにかかわらず、 bool に変換可能でなければなりません。 ただし VTInputIt の値型です。 また、 v を変更してはなりません。 そのため、引数の型 VT& は許されません。 また、 VT に対してムーブがコピーと同等でなければ VT も許されません。(C++11以上) ​​

q - 要求された要素に対して ​false を返す単項述語。

q(v)VT 型 (およびその const 修飾された型) のすべての引数 v について、その値カテゴリにかかわらず、 bool に変換可能でなければなりません。 ただし VTInputIt の値型です。 また、 v を変更してはなりません。 そのため、引数の型 VT& は許されません。 また、 VT に対してムーブがコピーと同等でなければ VT も許されません。(C++11以上) ​​

型の要件
-
InputItLegacyInputIterator の要件を満たさなければなりません。
-
ForwardItLegacyForwardIterator の要件を満たさなければなりません。
-
UnaryPredicatePredicate の要件を満たさなければなりません。

[編集]戻り値

条件を満たす最初の要素を指すイテレータ、またはそのような要素が見つからない場合は 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つが隣接している最初の位置を探します
(関数テンプレート)[edit]
指定された要素の並びが現れる最後の位置を探します
(関数テンプレート)[edit]
指定された要素のいずれかが現れる位置を探します
(関数テンプレート)[edit]
2つの範囲が異なる最初の位置を探します
(関数テンプレート)[edit]
指定範囲の要素に対して検索を行います
(関数テンプレート)[edit]
close