名前空間
変種
操作

std::find_first_of

From cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
制約付きアルゴリズムとRangeアルゴリズム (C++20)
制約付きアルゴリズム、例: ranges::copy, ranges::sort, ...
実行ポリシー (C++17)
シーケンスを変更しない操作
一括操作
(C++17)
検索操作
(C++11)                (C++11)(C++11)

シーケンスを変更する操作
コピー操作
(C++11)
(C++11)
スワップ操作
変換操作
生成操作
削除操作
順序変更操作
(C++17まで)(C++11)
(C++20)(C++20)
サンプリング操作
(C++17)

ソートおよび関連操作
パーティション操作
ソート操作
二分探索操作
(パーティション化された範囲)
集合操作 (ソート済み範囲)
マージ操作 (ソート済み範囲)
ヒープ操作
最小/最大操作
(C++11)
(C++17)
辞書順比較操作
順列操作
Cライブラリ
数値演算
未初期化メモリに対する操作
 
ヘッダー <algorithm> で定義
template< class InputIt, class ForwardIt >

InputIt find_first_of( InputIt first, InputIt last,

                       ForwardIt s_first, ForwardIt s_last );
(1) (C++20 以降 constexpr)
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以降)
template< class InputIt, class ForwardIt, class BinaryPred >

InputIt find_first_of( InputIt first, InputIt last,
                       ForwardIt s_first, ForwardIt s_last,

                       BinaryPred p );
(3) (C++20 以降 constexpr)
template< class ExecutionPolicy,

          class ForwardIt1, class ForwardIt2, class BinaryPred >
ForwardIt1 find_first_of( ExecutionPolicy&& policy,
                          ForwardIt1 first, ForwardIt last,
                          ForwardIt2 s_first, ForwardIt2 s_last,

                          BinaryPred p );
(4) (C++17以降)

範囲 [firstlast) の中で、範囲 [s_firsts_last) のいずれかの要素を検索します。

1) 要素は operator== を使用して比較されます。
3) 要素は、指定された二項述語 p を使用して比較されます。
2,4) (1,3)と同じだが、policyに従って実行される。
これらのオーバーロードは、以下のすべての条件が満たされた場合にのみオーバーロード解決に参加する。

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 の) Type1Type2 のすべての値を受け入れられる必要がある (したがって、Type1 & は許可されない。また、Type1 の場合、ムーブがコピーと同等でない限り、Type1 も許可されない。(C++11以降))。
Type1 および Type2 は、型 ForwardIt1 および ForwardIt2 のオブジェクトを逆参照し、それぞれ Type1 および Type2 に暗黙的に変換できる必要があります。 ​

型要件
-
InputItLegacyInputIterator の要件を満たす必要があります。
-
ForwardItLegacyForwardIterator の要件を満たさなければなりません。
-
ForwardIt1LegacyForwardIterator の要件を満たしている必要があります。
-
ForwardIt2LegacyForwardIterator の要件を満たしている必要があります。
-
BinaryPredBinaryPredicate の要件を満たす必要がある。

[編集] 戻り値

範囲 [firstlast) の中で、範囲 [s_firsts_last) の要素と等しい最初の要素へのイテレータ。

範囲 [s_firsts_last) が空であるか、そのような要素が見つからなかった場合は、 last が返されます。

[編集] 計算量

Nstd::distance(first, last) とし、 Sstd::distance(s_first, s_last) とすると、

1,2) N·S 回以下の operator== を使用した比較。
3,4) N·S 回以下の述語 p の適用。

[編集] 例外

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 firstlastLegacyForwardIterator である必要がありました。 それらは単に
LegacyInputIterator
LWG 1205 C++98 [s_firsts_last) が空の場合の戻り値が不明瞭でした。 この場合、last が返されます。

[編集] 関連項目

特定の基準を満たす最初の要素を見つける
(関数テンプレート) [編集]
要素の集合のうちいずれか1つを検索する
(アルゴリズム関数オブジェクト)[編集]
English 日本語 中文(简体) 中文(繁體)