std::find_first_of
| ヘッダー <algorithm> で定義 |
||
template< class InputIt, class ForwardIt > InputIt find_first_of( InputIt first, InputIt last, |
(1) | (C++20 以降 constexpr) |
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > ForwardIt1 find_first_of( ExecutionPolicy&& policy, |
(2) | (C++17以降) |
template< class InputIt, class ForwardIt, class BinaryPred > InputIt find_first_of( InputIt first, InputIt last, |
(3) | (C++20 以降 constexpr) |
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPred > |
(4) | (C++17以降) |
範囲 [first, last) の中で、範囲 [s_first, s_last) のいずれかの要素を検索します。
|
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である。 |
(C++20まで) |
|
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> は true です。 |
(C++20以降) |
目次 |
[編集] パラメータ
| first, last | - | 検査する要素の範囲を定義するイテレータのペア |
| s_first, s_last | - | 検索対象の要素の範囲を定義するイテレータのペア。 |
| policy | - | 使用する 実行ポリシー |
| p | - | 要素が等しいと見なされる場合に true を返す二項述語。 述語関数のシグネチャは、以下と同等である必要がある。 bool pred(const Type1 &a, const Type2 &b); シグネチャは const & を持つ必要はないが、関数は渡されたオブジェクトを変更してはならず、値カテゴリに関わらず、(おそらく const の) |
| 型要件 | ||
-InputIt は LegacyInputIterator の要件を満たす必要があります。 | ||
-ForwardIt は LegacyForwardIterator の要件を満たさなければなりません。 | ||
-ForwardIt1 は LegacyForwardIterator の要件を満たしている必要があります。 | ||
-ForwardIt2 は LegacyForwardIterator の要件を満たしている必要があります。 | ||
-BinaryPred は BinaryPredicate の要件を満たす必要がある。 | ||
[編集] 戻り値
範囲 [first, last) の中で、範囲 [s_first, s_last) の要素と等しい最初の要素へのイテレータ。
範囲 [s_first, s_last) が空であるか、そのような要素が見つからなかった場合は、 last が返されます。
[編集] 計算量
N を std::distance(first, last) とし、 S を std::distance(s_first, s_last) とすると、
[編集] 例外
ExecutionPolicy というテンプレートパラメータを持つオーバーロードは、次のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外をスローし、
ExecutionPolicyが 標準ポリシー のいずれかである場合、std::terminate が呼び出されます。その他のExecutionPolicyの場合、動作は実装定義です。 - アルゴリズムがメモリの割り当てに失敗した場合、std::bad_alloc がスローされます。
[編集] 可能な実装
| find_first_of (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; } |
| find_first_of (3) |
template<class InputIt, class ForwardIt, class BinaryPred> InputIt find_first_of(InputIt first, InputIt last, ForwardIt s_first, ForwardIt s_last, BinaryPred 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> auto print_sequence = [](const auto id, const auto& seq, int pos = -1) { std::cout << id << "{ "; for (int i{}; auto const& e : seq) { const bool mark{i == pos}; std::cout << (i++ ? ", " : ""); std::cout << (mark ? "[ " : "") << e << (mark ? " ]" : ""); } std::cout << " }\n"; }; int main() { const std::vector<int> v{0, 2, 3, 25, 5}; const auto t1 = {19, 10, 3, 4}; const auto t2 = {1, 6, 7, 9}; auto find_any_of = [](const auto& v, const auto& t) { const auto result = std::find_first_of(v.begin(), v.end(), t.begin(), t.end()); if (result == v.end()) { std::cout << "No elements of v are equal to any element of "; print_sequence("t = ", t); print_sequence("v = ", v); } else { const auto pos = std::distance(v.begin(), result); std::cout << "Found a match (" << *result << ") at position " << pos; print_sequence(", where t = ", t); print_sequence("v = ", v, pos); } }; find_any_of(v, t1); find_any_of(v, t2); }
出力
Found a match (3) at position 2, where t = { 19, 10, 3, 4 }
v = { 0, 2, [ 3 ], 25, 5 }
No elements of v are equal to any element of t = { 1, 6, 7, 9 }
v = { 0, 2, 3, 25, 5 }[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 576 | C++98 | first と last は LegacyForwardIterator である必要がありました。 | それらは単に LegacyInputIterator |
| LWG 1205 | C++98 | [s_first, s_last) が空の場合の戻り値が不明瞭でした。 |
この場合、last が返されます。 |
[編集] 関連項目
| (C++11) |
特定の基準を満たす最初の要素を見つける (関数テンプレート) |
| (C++20) |
要素の集合のうちいずれか1つを検索する (アルゴリズム関数オブジェクト) |