名稱空間
變體
操作

std::find, std::find_if, std::find_if_not

來自 cppreference.com
< cpp‎ | 演算法
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
定義於標頭檔案 <algorithm>
(1)
template< class InputIt, class T >
InputIt find( InputIt first, InputIt last, const T& value );
(C++20 起為 constexpr)
(直到 C++26)
template< class InputIt, class T = typename std::iterator_traits

                                       <InputIt>::value_type >

constexpr InputIt find( InputIt first, InputIt last, const T& value );
(C++26 起)
(2)
template< class ExecutionPolicy, class ForwardIt, class T >

ForwardIt find( ExecutionPolicy&& policy,

                ForwardIt first, ForwardIt last, const T& value );
(C++17 起)
(直到 C++26)
template< class ExecutionPolicy,

          class ForwardIt, class T = typename std::iterator_traits
                                         <ForwardIt>::value_type >
ForwardIt find( ExecutionPolicy&& policy,

                ForwardIt first, ForwardIt last, const T& value );
(C++26 起)
template< class InputIt, class UnaryPred >
InputIt find_if( InputIt first, InputIt last, UnaryPred p );
(3) (C++20 起為 constexpr)
template< class ExecutionPolicy, class ForwardIt, class UnaryPred >

ForwardIt find_if( ExecutionPolicy&& policy,

                   ForwardIt first, ForwardIt last, UnaryPred p );
(4) (C++17 起)
template< class InputIt, class UnaryPred >
InputIt find_if_not( InputIt first, InputIt last, UnaryPred q );
(5) (C++11 起)
(C++20 起為 constexpr)
template< class ExecutionPolicy, class ForwardIt, class UnaryPred >

ForwardIt find_if_not( ExecutionPolicy&& policy,

                       ForwardIt first, ForwardIt last, UnaryPred q );
(6) (C++17 起)

返回範圍 [firstlast) 中第一個滿足特定條件的元素(如果不存在這樣的迭代器,則返回 last)。

1) find 搜尋等於 value 的元素(使用 operator==)。
3) find_if 搜尋謂詞 p 返回 true 的元素。
5) find_if_not 搜尋謂詞 q 返回 false 的元素。
2,4,6)(1,3,5) 相同,但根據 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 - 定義要檢查的元素範圍的迭代器對
value - 用於比較元素的數值
policy - 要使用的 執行策略
p - 一元謂詞,對所需元素返回 true

表示式 p(v) 必須能轉換為 bool,其中 v 是型別為 (可能為 const) VT 的每個引數,VTInputIt 的值型別,無論 值類別如何,並且不得修改 v。因此,不允許引數型別為 VT&,除非對於 VT,移動等同於複製,否則也不允許 VT(C++11 起)。​

q - 一元謂詞,對所需元素返回 false

表示式 q(v) 必須能轉換為 bool,其中 v 是型別為 (可能為 const) VT 的每個引數,VTInputIt 的值型別,無論 值類別如何,並且不得修改 v。因此,不允許引數型別為 VT&,除非對於 VT,移動等同於複製,否則也不允許 VT(C++11 起)。​

型別要求
-
InputIt 必須滿足 LegacyInputIterator 的要求。
-
ForwardIt 必須滿足 LegacyForwardIterator 的要求。
-
UnaryPredicate 必須滿足 Predicate 的要求。

[編輯] 返回值

滿足以下條件,或如果不存在這樣的迭代器則返回 last範圍 [firstlast) 中的第一個迭代器 it

1,2) *it == valuetrue
3,4) p(*it)true
5,6) q(*it)false

[編輯] 複雜度

給定 N 作為 std::distance(first, last)

1,2) 最多 N 次與 value 使用 operator== 進行比較。
3,4) 最多 N 次謂詞 p 的應用。
5,6) 最多 N 次謂詞 q 的應用。

[編輯] 異常

帶有模板引數 ExecutionPolicy 的過載按如下方式報告錯誤

  • 如果作為演算法一部分呼叫的函式執行丟擲異常,並且 ExecutionPolicy標準策略之一,則呼叫 std::terminate。對於任何其他 ExecutionPolicy,行為由實現定義。
  • 如果演算法未能分配記憶體,則丟擲 std::bad_alloc

[編輯] 可能實現

find (1)
template<class InputIt, class T = typename std::iterator_traits<InputIt>::value_type>
constexpr InputIt find(InputIt first, InputIt last, const T& value)
{
    for (; first != last; ++first)
        if (*first == value)
            return first;
 
    return last;
}
find_if (3)
template<class InputIt, class UnaryPred>
constexpr InputIt find_if(InputIt first, InputIt last, UnaryPred p)
{
    for (; first != last; ++first)
        if (p(*first))
            return first;
 
    return last;
}
find_if_not (5)
template<class InputIt, class UnaryPred>
constexpr InputIt find_if_not(InputIt first, InputIt last, UnaryPred q)
{
    for (; first != last; ++first)
        if (!q(*first))
            return first;
 
    return last;
}

[編輯] 注意

如果 C++11 不可用,std::find_if_not 的等效用法是使用帶否定謂詞的 std::find_if

template<class InputIt, class UnaryPred>
InputIt find_if_not(InputIt first, InputIt last, UnaryPred q)
{
    return std::find_if(first, last, std::not1(q));
}
特性測試 標準 特性
__cpp_lib_algorithm_default_value_type 202403 (C++26) 演算法的列表初始化 (1,2)

[編輯] 示例

以下示例在給定序列中查詢數字。

#include <algorithm>
#include <array>
#include <cassert>
#include <complex>
#include <initializer_list>
#include <iostream>
#include <vector>
 
bool is_even(int i)
{
    return i % 2 == 0;
}
 
void example_contains()
{
    const auto haystack = {1, 2, 3, 4};
 
    for (const int needle : {3, 5})
        if (std::find(haystack.begin(), haystack.end(), needle) == haystack.end())
            std::cout << "haystack does not contain " << needle << '\n';
        else
            std::cout << "haystack contains " << needle << '\n';
}
 
void example_predicate()
{
    for (const auto& haystack : {std::array{3, 1, 4}, {1, 3, 5}})
    {
        const auto it = std::find_if(haystack.begin(), haystack.end(), is_even);
        if (it != haystack.end())
            std::cout << "haystack contains an even number " << *it << '\n';
        else
            std::cout << "haystack does not contain even numbers\n";
    }
}
 
void example_list_init()
{
    std::vector<std::complex<double>> haystack{{4.0, 2.0}};
#ifdef __cpp_lib_algorithm_default_value_type
    // T gets deduced making list-initialization possible
    const auto it = std::find(haystack.begin(), haystack.end(), {4.0, 2.0});
#else
    const auto it = std::find(haystack.begin(), haystack.end(), std::complex{4.0, 2.0});
#endif
    assert(it == haystack.begin());  
}
 
int main()
{
    example_contains();
    example_predicate();
    example_list_init();
}

輸出

haystack contains 3
haystack does not contain 5
haystack contains an even number 4
haystack does not contain even numbers

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 283 C++98 要求 TEqualityComparable,但
InputIt 的值型別可能不是 T
移除了此要求

[編輯] 另請參閱

尋找第一對相等的(或滿足給定謂詞的)相鄰項
(函式模板) [編輯]
在特定範圍中尋找最後一次出現的元素序列
(函式模板) [編輯]
搜尋一組元素中的任何一個
(函式模板) [編輯]
尋找兩個範圍開始不同的第一個位置
(函式模板) [編輯]
搜尋一個範圍的元素首次出現的位置
(函式模板) [編輯]
尋找第一個滿足特定條件的元素
(演算法函式物件)[編輯]