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 ); |
(constexpr since C++20) (until C++26) |
|
template< class InputIt, class T = typename std::iterator_traits <InputIt>::value_type > |
(since C++26) | |
(2) | ||
template< class ExecutionPolicy, class ForwardIt, class T > ForwardIt find( ExecutionPolicy&& policy, |
(since C++17) (until C++26) |
|
template< class ExecutionPolicy, class ForwardIt, class T = typename std::iterator_traits |
(since C++26) | |
template< class InputIt, class UnaryPred > InputIt find_if( InputIt first, InputIt last, UnaryPred p ); |
(3) | (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred > ForwardIt find_if( ExecutionPolicy&& policy, |
(4) | (since C++17) |
template< class InputIt, class UnaryPred > InputIt find_if_not( InputIt first, InputIt last, UnaryPred q ); |
(5) | (since C++11) (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred > ForwardIt find_if_not( ExecutionPolicy&& policy, |
(6) | (since 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。 |
(until C++20) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> 为 true。 |
(since C++20) |
目录 |
[编辑] 参数
first, last | - | 定义要检查的元素范围的迭代器对 |
value | - | 要与之比较元素的数值 |
policy | - | 要使用的执行策略 |
p | - | 一元谓词,对于所需的元素返回 true。 对于类型为 (可能为 const) |
q | - | 一元谓词,对于所需的元素返回 false。 对于类型为 (可能为 const) |
类型要求 | ||
-InputIt 必须满足 LegacyInputIterator 的要求。 | ||
-ForwardIt 必须满足 LegacyForwardIterator 的要求。 | ||
-UnaryPredicate 必须满足 Predicate 的要求。 |
[编辑] 返回值
范围 [
first,
last)
中首个满足以下条件的迭代器 it,如果不存在这样的迭代器,则返回 last
[编辑] 复杂度
给定 N 为 std::distance(first, last)
operator==
与 value 进行比较。[编辑] 异常
带有名为 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)); } |
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__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++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 283 | C++98 | T 需要是 EqualityComparable,但InputIt 的值类型可能不是 T |
移除了该要求 |
[编辑] 参见
查找首两个相邻的相等项(或满足给定谓词的项) (函数模板) | |
查找特定范围内元素的最后一个序列 (函数模板) | |
搜索一组元素中的任何一个 (函数模板) | |
查找两个范围不同的第一个位置 (函数模板) | |
搜索某个元素范围的首次出现 (函数模板) | |
(C++20)(C++20)(C++20) |
查找满足特定条件的第一个元素 (算法函数对象) |