std::find_first_of
ヘッダ <algorithm> で定義
|
||
(1) | ||
template< class ForwardIt1, class ForwardIt2 > ForwardIt1 find_first_of( ForwardIt1 first, ForwardIt1 last, |
(C++11未満) | |
template< class InputIt, class ForwardIt > InputIt find_first_of( InputIt first, InputIt last, |
(C++11以上) (C++20未満) |
|
template< class InputIt, class ForwardIt > constexpr InputIt find_first_of( InputIt first, InputIt last, |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > ForwardIt1 find_first_of( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, |
(2) | (C++17以上) |
(3) | ||
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > ForwardIt1 find_first_of( ForwardIt1 first, ForwardIt1 last, |
(C++11未満) | |
template< class InputIt, class ForwardIt, class BinaryPredicate > InputIt find_first_of( InputIt first, InputIt last, |
(C++11以上) (C++20未満) |
|
template< class InputIt, class ForwardIt, class BinaryPredicate > constexpr InputIt find_first_of( InputIt first, InputIt last, |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPredicate > ForwardIt1 find_first_of( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt last, |
(4) | (C++17以上) |
範囲 [s_first, s_last)
内のいずれかの要素を範囲 [first, last)
から検索します。
operator==
を用いて比較されます。p
を用いて比較されます。policy
に従って実行されます。 このオーバーロードは、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である場合にのみ、オーバーロード解決に参加します目次 |
[編集] 引数
first, last | - | 調べる要素の範囲 |
s_first, s_last | - | 検索する要素の範囲 |
policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
p | - | 要素が等しいと扱われるべき場合に true を返す二項述語。 述語関数のシグネチャは以下と同等なものであるべきです。 bool pred(const Type1 &a, const Type2 &b); シグネチャが const & を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはなならず、値カテゴリに関わらず |
型の要件 | ||
-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; } |
[編集] 例
以下のコードは整数のベクタから指定された整数のいずれかを検索します。
#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) |
一定の基準を満たす最初の要素を探します (関数テンプレート) |