std::find, std::find_if, std::find_if_not
定义于头文件 <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 > |
(C++26 起) | |
(2) | ||
template< class ExecutionPolicy, class ForwardIt, class T > ForwardIt find( ExecutionPolicy&& policy, |
(C++17 起) (直到 C++26) |
|
template< class ExecutionPolicy, class ForwardIt, class T = typename std::iterator_traits |
(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, |
(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, |
(6) | (C++17 起) |
返回范围 [
first,
last)
中第一个满足特定条件的元素(如果不存在这样的迭代器,则返回 last)。
find
搜索等于 value 的元素(使用 operator==
)。find_if
搜索谓词 p 返回 true 的元素。find_if_not
搜索谓词 q 返回 false 的元素。
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,其中 |
q | - | 一元谓词,对所需元素返回 false。 表达式 q(v) 必须能转换为 bool,其中 |
类型要求 | ||
-InputIt 必须满足 LegacyInputIterator 的要求。 | ||
-ForwardIt 必须满足 LegacyForwardIterator 的要求。 | ||
-UnaryPredicate 必须满足 Predicate 的要求。 |
[编辑] 返回值
满足以下条件,或如果不存在这样的迭代器则返回 last 的 范围 [
first,
last)
中的第一个迭代器 it
[编辑] 复杂度
给定 N 作为 std::distance(first, last)
operator==
进行比较。[编辑] 异常
带有模板参数 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 | 要求 T 为 EqualityComparable,但InputIt 的值类型可能不是 T |
移除了此要求 |
[编辑] 另请参阅
寻找第一对相等的(或满足给定谓词的)相邻项 (函数模板) | |
在特定范围中寻找最后一次出现的元素序列 (函数模板) | |
搜索一组元素中的任何一个 (函数模板) | |
寻找两个范围开始不同的第一个位置 (函数模板) | |
搜索一个范围的元素首次出现的位置 (函数模板) | |
(C++20)(C++20)(C++20) |
寻找第一个满足特定条件的元素 (算法函数对象) |