在线文档教程
C++
算法 | Algorithm

std::find_first_of

STD:找到[医]第一[医]成

Defined in header
(1)
template< class ForwardIt1, class ForwardIt2 > ForwardIt1 find_first_of( ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last (until C++11)
template< class InputIt, class ForwardIt > InputIt find_first_of( InputIt first, InputIt last, ForwardIt s_first, ForwardIt s_last (since C++11)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > ForwardIt1 find_first_of( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last (2)(since C++17)
(3)
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > ForwardIt1 find_first_of( ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p (until C++11)
template< class InputIt, class ForwardIt, class BinaryPredicate > InputIt find_first_of( InputIt first, InputIt last, ForwardIt s_first, ForwardIt s_last, BinaryPredicate p (since C++11)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPredicate > ForwardIt1 find_first_of( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt last, ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p (4)(since C++17)

搜索范围[first, last)对于范围内的任何元素[s_first, s_last)...

1%29个元素的比较operator==...

使用给定的二进制谓词对3%29个元素进行比较。p...

2,4%29与%281,3%29相同,但根据policy。此重载只参与以下情况下的过载解决方案:std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

参数

first, last-the range of elements to examine
s_first, s_last-the range of elements to search for
policy-the execution policy to use. See execution policy for details.
p-binary predicate which returns ​true if the elements should be treated as equal. The signature of the predicate function should be equivalent to the following: bool pred(const Type1 &a, const Type2 &b The signature does not need to have const &, but the function must not modify the objects passed to it. The types Type1 and Type2 must be such that objects of types ForwardIt1 and ForwardIt2 can be dereferenced and then implicitly converted to Type1 and Type2 respectively. ​

类型要求

-输入必须符合输入器的要求。

-前进1必须符合先行者的要求。

-前进2必须符合先行者的要求。

返回值

范围内第一个元素的迭代器[first, last),它等于来自范围的元素。[s_first; s_last)如果没有找到这类元素,last会被归还。

复杂性

充其量(S*N)比较S = distance(s_first, s_last)N = distance(first, last)...

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。

  • 如果算法不能分配内存,std::bad_alloc被扔了。

可能的实施

第一版

*。

模板<类InputIt,类Forwardit>InputIt找到[医]第一[医]在%28的第一,输入最后,前进是[医]第一,前进[医]最后%29(表示%28;第一%21=最后;++第29%(表示%28前进)[医]首先;它%21=s[医]最后;++它%29{如果%28%2A第一==%2A它%29{先返回;}}最后返回;}

第二版

模板<类Inputit,类向前,类BinaryPredicate>InputIt找到[医]第一[医]在%28的第一,输入最后,前进是[医]第一,前进[医]最后,二进制预测p%29{表示%28;第一%21=最后;++第一%29{表示%28前进it=s[医]首先;它%21=s[医]最后;++它%29{如果%28 p%28%2A首先,%2A它%29%29{先返回;}}返回最后;}

以下代码在整数向量中搜索任何指定的整数:

二次

#include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> v{0, 2, 3, 25, 5}; std::vector<int> t{3, 19, 10, 2}; auto result = std::find_first_of(v.begin(), v.end(), t.begin(), t.end() if (result == v.end()) { std::cout << "no elements of v were equal to 3, 19, 10 or 2\n"; } else { std::cout << "found a match at " << std::distance(v.begin(), result) << "\n"; } }

二次

产出:

二次

found a match at 1

二次

另见

findfind_iffind_if_not (C++11)finds the first element satisfying specific criteria (function template)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/Algorithm/find[医]第一[医]成