std::find_first_of
提供: 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>
| ヘッダ <algorithm> で定義
|
||
| (1) | ||
template< class ForwardIt1, class ForwardIt2 > ForwardIt1 find_first_of( ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last ); |
(C++11未満) | |
template< class InputIt, class ForwardIt > InputIt find_first_of( InputIt first, InputIt last, ForwardIt s_first, ForwardIt s_last ); |
(C++11以上) (C++20未満) |
|
template< class InputIt, class ForwardIt > constexpr InputIt find_first_of( InputIt first, InputIt last, ForwardIt s_first, ForwardIt s_last ); |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > ForwardIt1 find_first_of( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last ); |
(2) | (C++17以上) |
| (3) | ||
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > ForwardIt1 find_first_of( ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p ); |
(C++11未満) | |
template< class InputIt, class ForwardIt, class BinaryPredicate > InputIt find_first_of( InputIt first, InputIt last, ForwardIt s_first, ForwardIt s_last, BinaryPredicate p ); |
(C++11以上) (C++20未満) |
|
template< class InputIt, class ForwardIt, class BinaryPredicate > constexpr InputIt find_first_of( InputIt first, InputIt last, ForwardIt s_first, ForwardIt s_last, BinaryPredicate p ); |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPredicate > ForwardIt1 find_first_of( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt last, ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p ); |
(4) | (C++17以上) |
範囲 [s_first, s_last) 内のいずれかの要素を範囲 [first, last) から検索します。
1) 要素は
operator== を用いて比較されます。3) 要素は指定された二項述語
p を用いて比較されます。2,4) (1,3) と同じですが、
policy に従って実行されます。 このオーバーロードは、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である場合にのみ、オーバーロード解決に参加します引数
| first, last | - | 調べる要素の範囲 |
| s_first, s_last | - | 検索する要素の範囲 |
| policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
| p | - | 要素が等しいと扱われるべき場合に true を返す二項述語。 述語関数のシグネチャは以下と同等なものであるべきです。
シグネチャが |
| 型の要件 | ||
-InputIt は LegacyInputIterator の要件を満たさなければなりません。
| ||
-ForwardIt1 は LegacyForwardIterator の要件を満たさなければなりません。
| ||
-ForwardIt2 は LegacyForwardIterator の要件を満たさなければなりません。
| ||
戻り値
範囲 [s_first; s_last) 内の要素と等しい、範囲 [first, last) 内の最初の要素を指すイテレータ。 そのような要素が見つからない場合は、 last が返されます。
計算量
多くとも (S*N) 回の比較。 ただし S = distance(s_first, s_last)、 N = distance(first, last) です。
例外
テンプレート引数 ExecutionPolicy を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicyが標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆるExecutionPolicyについては、動作は処理系定義です。 - アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。
実装例
| 1つめのバージョン |
|---|
template<class InputIt, class ForwardIt>
InputIt find_first_of(InputIt first, InputIt last,
ForwardIt s_first, ForwardIt s_last)
{
for (; first != last; ++first) {
for (ForwardIt it = s_first; it != s_last; ++it) {
if (*first == *it) {
return first;
}
}
}
return last;
}
|
| 2つめのバージョン |
template<class InputIt, class ForwardIt, class BinaryPredicate>
InputIt find_first_of(InputIt first, InputIt last,
ForwardIt s_first, ForwardIt s_last,
BinaryPredicate p)
{
for (; first != last; ++first) {
for (ForwardIt it = s_first; it != s_last; ++it) {
if (p(*first, *it)) {
return first;
}
}
}
return last;
}
|
例
以下のコードは整数のベクタから指定された整数のいずれかを検索します。
Run this code
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v{0, 2, 3, 25, 5};
std::vector<int> t{3, 19, 10, 2};
auto result = std::find_first_of(v.begin(), v.end(), t.begin(), t.end());
if (result == v.end()) {
std::cout << "no elements of v were equal to 3, 19, 10 or 2\n";
} else {
std::cout << "found a match at "
<< std::distance(v.begin(), result) << "\n";
}
}
出力:
found a match at 1
関連項目
(C++11) |
一定の基準を満たす最初の要素を探します (関数テンプレート) |