名前空間
変種
操作

std::find_first_of

提供: cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
制約付きアルゴリズムと範囲に対するアルゴリズム (C++20)
コンセプトとユーティリティ: std::sortable, std::projected, ...
制約付きアルゴリズム: std::ranges::copy, std::ranges::sort, ...
実行ポリシー (C++17)
非変更シーケンス操作
(C++11)(C++11)(C++11)
(C++17)
find_first_of
変更シーケンス操作
未初期化記憶域の操作
分割操作
ソート操作
(C++11)
二分探索操作
集合操作 (ソート済み範囲用)
ヒープ操作
(C++11)
最小/最大演算
(C++11)
(C++17)

順列
数値演算
C のライブラリ
 
ヘッダ <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 を返す二項述語。

述語関数のシグネチャは以下と同等なものであるべきです。

 bool pred(const Type1 &a, const Type2 &b);

シグネチャが const & を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはなならず、値カテゴリに関わらず Type1 および Type2 型 (およびそれらの const 修飾された型) のすべての値を受理できなければなりません (そのため Type1 & は許されません。 また Type1 に対してムーブがコピーと同等でなければ Type1 も許されません (C++11以上))。
Type1 および Type2 は、 ForwardIt1 および ForwardIt2 型のオブジェクトの逆参照からType1 および Type2 にそれぞれ暗黙に変換可能なものでなければなりません。 ​

型の要件
-
InputItLegacyInputIterator の要件を満たさなければなりません。
-
ForwardIt1LegacyForwardIterator の要件を満たさなければなりません。
-
ForwardIt2LegacyForwardIterator の要件を満たさなければなりません。

[編集] 戻り値

範囲 [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

[編集] 関連項目

一定の基準を満たす最初の要素を探します
(関数テンプレート) [edit]