The Wayback Machine - https://web.archive.org/web/20180325201021/http://ja.cppreference.com:80/w/cpp/algorithm/find
名前空間
変種
操作

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

提供: cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
実行ポリシー (C++17)
非変更シーケンス操作
(C++11)(C++11)(C++11)
(C++17)
findfind_iffind_if_not
(C++11)
変更シーケンス操作
未初期化記憶域の操作
分割操作
ソート操作
バイナリサーチ操作
集合操作 (ソート済み範囲に対する)
ヒープ操作
(C++11)
最小/最大演算
(C++11)
(C++17)
順列
数値演算
C のライブラリ
 
ヘッダ <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,

                 UnaryPredicate p );
(2)
template<class InputIt, class UnaryPredicate >

InputIt find_if_not( InputIt first, InputIt last,

                     UnaryPredicate q );
(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& を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはなりません。
Type は型 InputIt のオブジェクトの逆参照から暗黙に変換可能なものでなければなりません。 ​

q - 必須要素のために ​false を返す単項述語。

述語関数のシグネチャは以下と同等なものであるべきです。

 bool pred(const Type &a);

シグネチャが const& を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはなりません。
Type は型 InputIt のオブジェクトの逆参照から暗黙に変換可能なものでなければなりません。 ​

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

[編集]返り値

条件を満たす最初の要素へのイテレータ。そのような要素がみつからない場合は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.
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.

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