std::find, std::find_if, std::find_if_not
提供: cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| ヘッダ <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)
find は value と等しい要素を検索します。3)
find_if は述語 p が true を返す要素を検索します。5)
find_if_not は述語 q が false を返す要素を検索します。2,4,6) (1,3,5) と同じですが、
policy に従って実行されます。 このオーバーロードは、std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である場合にのみ、オーバーロード解決に参加します引数
| first, last | - | 調べる要素の範囲 |
| value | - | 要素と比較する値 |
| policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
| p | - | 要求された要素に対して true を返す単項述語。 式 |
| q | - | 要求された要素に対して false を返す単項述語。 式 |
| 型の要件 | ||
-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));
}
|
例
以下の例は整数のベクタから整数を探します。
Run this code
#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つの範囲が異なる最初の位置を探します (関数テンプレート) | |
| 指定範囲の要素に対して検索を行います (関数テンプレート) |