From cppreference.com
Defined in header
template<classInputIt,classForwardIt>InputItfind_first_of(InputItfirst1,InputItlast1,ForwardItfirst2,ForwardItlast2); (1)(constexpr since C++20)template<classInputIt,classForwardIt,classBinaryPred>InputItfind_first_of(InputItfirst1,InputItlast1,ForwardItfirst2,ForwardItlast2,BinaryPredp); (2)(constexpr since C++20)template<classExecutionPolicy,classForwardIt1,classForwardIt2>ForwardIt1find_first_of(ExecutionPolicy&&policy,ForwardIt1first1,ForwardIt1last1,ForwardIt2first2,ForwardIt2last2); (3) (since C++17)template<classExecutionPolicy,classForwardIt1,classForwardIt2,classBinaryPred>ForwardIt1find_first_of(ExecutionPolicy&&policy,ForwardIt1first1,ForwardItlast1,ForwardIt2first2,ForwardIt2last2,BinaryPredp); (4) (since C++17)Searches the source range [first1, last1) for any of the elements in the target range [first2, last2).
1) Elements are compared using operator==.
2) Elements are compared using the given binary predicate p.
3,4) Same as (1,2), but executed according to policy.
These overloads participate in overload resolution only if the value of the following expression is true:
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>
(until C++20)std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>
(since C++20)Parameters
first1, last1 - the pair of iterators defining the source
first2, last2 - the pair of iterators defining the target
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:
boolpred(constType1&a,constType2&b);
While the signature does not need to have const&, the function must not modify the objects passed to it and must be able to accept all values of type (possibly const) Type1 and Type2 regardless of
(thus, Type1& is not allowed, nor is Type1 unless for Type1 a move is equivalent to a copy(since C++11)).
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.
policy - the
to use Type requirements -InputIt must meet the requirements of
. -ForwardIt must meet the requirements of
. -ForwardIt1 must meet the requirements of
. -ForwardIt2 must meet the requirements of
. -BinaryPred must meet the requirements of
. Return value
Iterator to the first element in the source range that matches an element from the target range.
If the target range is empty or if no such element is found, last1 is returned.
Complexity
Given N1 as std::distance(first1,last1) and N2 as std::distance(first2,last2):
1) At most N1⋅N2 comparisons using operator==.
2) At most N1⋅N2 applications of p.
3)𝓞(N1⋅N2) comparisons using operator==.
4)𝓞(N1⋅N2) applications of p.
Exceptions
3,4) During the execution process:
If the temporary memory resources required for parallelization are not available,
is thrown.
If an uncaught exception is thrown while accessing objects via an algorithm argument, the behavior is determined by the execution policy (for
,
is invoked).
Possible implementation
template<classInputIt,classForwardIt>InputItfind_first_of(InputItfirst1,InputItlast1,ForwardItfirst2,ForwardItlast2){for(;first1!=last1;++first1)for(ForwardItit=first2;it!=last2;++it)if(*first1==*it)returnfirst1;returnlast1;}
template<classInputIt,classForwardIt,classBinaryPred>InputItfind_first_of(InputItfirst1,InputItlast1,ForwardItfirst2,ForwardItlast2,BinaryPredp){for(;first1!=last1;++first1)for(ForwardItit=first2;it!=last2;++it)if(p(*first1,*it))returnfirst1;returnlast1;}Example
The following code searches for any of specified integers in a vector of integers:
Run this code
#include<algorithm>#include<iostream>#include<vector>autoprint_sequence=[](constautoid,constauto&seq,intpos=-1){std::cout<<id<<"{ ";for(inti{};constauto&e:seq){constboolmark{i==pos};std::cout<<(i++?", ":"");std::cout<<(mark?"[ ":"")<<e<<(mark?" ]":"");}std::cout<<" }\n";};intmain(){conststd::vector<int>v{0,2,3,25,5};constautot1={19,10,3,4};constautot2={1,6,7,9};autofind_any_of=[](constauto&v,constauto&t){constautoresult=std::find_first_of(v.begin(),v.end(),t.begin(),t.end());if(result==v.end()){std::cout<<"No elements of v are equal to any element of ";print_sequence("t = ",t);print_sequence("v = ",v);}else{constautopos=std::distance(v.begin(),result);std::cout<<"Found a match ("<<*result<<") at position "<<pos;print_sequence(", where t = ",t);print_sequence("v = ",v,pos);}};find_any_of(v,t1);find_any_of(v,t2);}Output:
Found a match (3) at position 2, where t = { 19, 10, 3, 4 } v = { 0, 2, [ 3 ], 25, 5 } No elements of v are equal to any element of t = { 1, 6, 7, 9 } v = { 0, 2, 3, 25, 5 } Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++98 first1 and last1 needed to be
they only need to be
C++98 the return value was unclear if the target range is empty returns last1 in this case See also