std::find, std::find_if, std::find_if_not
提供: cppreference.com
ヘッダ <algorithm> で定義 | ||
template<class InputIt, class T > InputIt find( InputIt first, InputIt last, const T& value ); | (1) | |
template<class InputIt, class UnaryPredicate > InputIt find_if( InputIt first, InputIt last, | (2) | |
template<class InputIt, class UnaryPredicate > InputIt find_if_not( InputIt first, InputIt last, | (3) | (C++11およびそれ以降) |
これらの関数は、特定の条件を満たす範囲[first, last)
内の最初の要素を見つけます。
1. find
は値value
に等しい要素を検索します。
2. find_if
は述語p
の戻り値がtrueの要素を検索します。
3. find_if_not
は述語q
の戻り値がfalseの要素を検索します。
目次 |
[編集]パラメータ
first, last | - | 検索する要素の範囲 |
value | - | 要素と比較する値 |
p | - | 必須要素のために true を返す単項述語。 述語関数のシグネチャは以下と同等なものであるべきです。 bool pred(const Type &a); シグネチャが const& を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはなりません。 |
q | - | 必須要素のために false を返す単項述語。 述語関数のシグネチャは以下と同等なものであるべきです。 bool pred(const Type &a); シグネチャが const& を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはなりません。 |
型の要件 | ||
-InputIt は InputIterator の要件を満たさなければなりません。 |
[編集]返り値
条件を満たす最初の要素へのイテレータ。そのような要素がみつからない場合はlast
[編集]計算複雑性
せいぜいlast
- 述語のfirst
アプリケーション
[編集]可能な実装
1つめのバージョン |
---|
template<class InputIt, class T> 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> 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> 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を使用することです.
Original:
If you do not have C++11, an equivalent to std::find_if_not is to use std::find_if with the negated predicate.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
template<class InputIt, class UnaryPredicate> InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q){return std::find_if(first, last, std::not1(q));} |
[編集]例
次の例では、整数のベクトルでの整数を求め.
Original:
The following example finds an integer in a vector of integers.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Run this code
#include <iostream>#include <algorithm>#include <vector> int main(){int n1 =3;int n2 =5; std::vector<int> v{0, 1, 2, 3, 4}; auto result1 = std::find(v.begin(), v.end(), n1);auto result2 = std::find(v.begin(), v.end(), n2); if(result1 != v.end()){std::cout<<"v contains: "<< n1 <<'\n';}else{std::cout<<"v does not contain: "<< n1 <<'\n';} if(result2 != v.end()){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つの範囲が異なる最初の位置を探します (関数テンプレート) | |
指定範囲の要素に対して検索を行います (関数テンプレート) |