std::find_first_of
在标头 <algorithm> 定义 | ||
template<class InputIt, class ForwardIt > InputIt find_first_of( InputIt first, InputIt last, | (1) | (C++20 起为 constexpr ) |
template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > ForwardIt1 find_first_of( ExecutionPolicy&& policy, | (2) | (C++17 起) |
template<class InputIt, class ForwardIt, class BinaryPred > InputIt find_first_of( InputIt first, InputIt last, | (3) | (C++20 起为 constexpr ) |
template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPred > | (4) | (C++17 起) |
在范围 [
first,
last)
中搜索范围 [
s_first,
s_last)
中的任何元素。
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& ,函数也不能修改传递给它的对象,而且必须接受(可为 const 的)类型 |
类型要求 | ||
-InputIt 必须满足老式输入迭代器(LegacyInputIterator) 。 | ||
-ForwardIt 必须满足老式向前迭代器(LegacyForwardIterator) 。 | ||
-ForwardIt1 必须满足老式向前迭代器(LegacyForwardIterator) 。 | ||
-ForwardIt2 必须满足老式向前迭代器(LegacyForwardIterator) 。 | ||
-BinaryPredicate 必须满足二元谓词(BinaryPredicate) 。 |
[编辑]返回值
指向范围 [
first,
last)
中等于范围 [
s_first,
s_last)
中某个元素的首个元素。
如果 [
s_first,
s_last)
为空或找不到这种元素,那么就会返回 last。
[编辑]复杂度
给定 N 为 std::distance(first, last),S 为 std::distance(s_first, s_last):
[编辑]异常
拥有名为 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;} |
[编辑]示例
下列代码在包含整数的 vector 中搜索任何一个指定的整数:
#include <algorithm>#include <iostream>#include <vector> auto print_sequence =[](constauto id, constauto& seq, int pos =-1){std::cout<< id <<"{ ";for(int i{};autoconst& e : seq){constbool mark{i == pos};std::cout<<(i++?", ":"");std::cout<<(mark ?"[ ":"")<< e <<(mark ?" ]":"");}std::cout<<" }\n";}; int main(){conststd::vector<int> v{0, 2, 3, 25, 5};constauto t1 ={19, 10, 3, 4};constauto t2 ={1, 6, 7, 9}; auto find_any_of =[](constauto& v, constauto& t){constauto result = std::find_first_of(v.begin(), v.end(), t.begin(), t.end());if(result == v.end()){std::cout<<"v 和 t 中没有相等的元素\n"; print_sequence("t = ", t); print_sequence("v = ", v);}else{constauto pos =std::distance(v.begin(), result);std::cout<<"在位置 "<< pos <<" 找到匹配 ("<<*result <<")\n"; print_sequence(", where t = ", t); print_sequence("v = ", v, pos);}}; find_any_of(v, t1); find_any_of(v, t2);}
输出:
在位置 2 找到匹配 (3) t = { 19, 10, 3, 4 } v = { 0, 2, [ 3 ], 25, 5 } v 和 t 中没有相等的元素 t = { 1, 6, 7, 9 } v = { 0, 2, 3, 25, 5 }
[编辑]缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
LWG 576 | C++98 | first 和 last 需要是老式向前迭代器(LegacyForwardIterator) | 只需要是老式输入迭代器(LegacyInputIterator) |
LWG 1205 | C++98 | [ s_first, s_last) 为空时返回值不明确 | 此时会返回 last |
[编辑]参阅
(C++11) | 查找首个满足特定条件的元素 (函数模板) |
(C++20) | 搜索一组元素中任一元素 (算法函数对象) |