std::find, std::find_if, std::find_if_not
| Defined in header <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 | - | unary predicate which returns true 必須要素のために. The signature of the predicate function should be equivalent to the following: bool pred(const Type &a); The signature does not need to have const &, but the function must not modify the objects passed to it. |
| q | - | unary predicate which returns false 必須要素のために. The signature of the predicate function should be equivalent to the following: bool pred(const Type &a); The signature does not need to have const &, but the function must not modify the objects passed to it. |
| 型の要件 | ||
-InputIt は InputIterator
の要求を満足しなければなりません。 | ||
[編集] 返り値
条件を満たす最初の要素へのイテレータ。そのような要素がみつからない場合はlast
[編集] 計算複雑性
せいぜいlast - 述語のfirstアプリケーション
[編集] 可能な実装
| First version |
|---|
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; } |
| Second version |
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; } |
| Third version |
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; } |
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)); } |
[編集] 例
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つの同一の(またはいくつかの他の関係)のアイテムを見つけることができます Original: finds two identical (or some other relationship) items adjacent to each other The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (関数テンプレート) | |
| 一定の範囲内にある要素の最後のシーケンスを見つけることができます Original: finds the last sequence of elements in a certain range The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (関数テンプレート) | |
| searches for any one of a set of elements (関数テンプレート) | |
| 2の範囲が異なる最初の位置を見つけます Original: finds the first position where two ranges differ The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (関数テンプレート) | |
| ある範囲の要素を検索します (関数テンプレート) | |