名稱空間
變體
操作

std::find_first_of

來自 cppreference.com
< cpp‎ | 演算法
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (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 &,但該函式不得修改傳遞給它的物件,並且必須能夠接受 `Type1` 和 `Type2` 型別(可能為 const)的所有值,無論其 值類別如何(因此,不允許使用 Type1 &,也不允許使用 Type1,除非對於 `Type1`,移動等同於複製(C++11 起))。
型別 Type1Type2 必須使得型別為 ForwardIt1ForwardIt2 的物件可以被解引用,然後分別隱式轉換為 Type1Type2。​

型別要求
-
InputIt 必須滿足 LegacyInputIterator 的要求。
-
ForwardIt 必須滿足 LegacyForwardIterator 的要求。
-
ForwardIt1 必須滿足 LegacyForwardIterator 的要求。
-
ForwardIt2 必須滿足 LegacyForwardIterator 的要求。
-
BinaryPred 必須滿足 BinaryPredicate 的要求。

[編輯] 返回值

指向範圍 [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++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 576 C++98 firstlast 需要是 LegacyForwardIterators 它們只需要是
LegacyInputIterators
LWG 1205 C++98 如果 [s_firsts_last) 為空,返回值不明確 在這種情況下返回 last

[編輯] 另請參閱

尋找第一個滿足特定條件的元素
(函式模板) [編輯]
搜尋一組元素中的任何一個
(演算法函式物件)[編輯]