| Defined in header <algorithm>
|
||
template< class InputIt, class ForwardIt >
InputIt find_first_of( InputIt first1, InputIt last1,
ForwardIt first2, ForwardIt last2 );
|
(1) | (constexpr since C++20) |
template< class InputIt, class ForwardIt, class BinaryPred >
InputIt find_first_of( InputIt first1, InputIt last1,
ForwardIt first2, ForwardIt last2,
BinaryPred p );
|
(2) | (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
ForwardIt1 find_first_of( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2 );
|
(3) | (since C++17) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2, class BinaryPred >
ForwardIt1 find_first_of( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt last1,
ForwardIt2 first2, ForwardIt2 last2,
BinaryPred p );
|
(4) | (since C++17) |
Searches the source range [first1, last1) for any of the elements in the target range [first2, last2).
operator==.p.policy.true:
|
|
(until C++20) |
|
|
(since C++20) |
Parameters
| first1, last1 | - | the pair of iterators defining the source range |
| first2, last2 | - | the pair of iterators defining the target range |
| 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:
While the signature does not need to have |
| policy | - | the execution policy to use |
| Type requirements | ||
-InputIt must meet the requirements of LegacyInputIterator.
| ||
-ForwardIt must meet the requirements of LegacyForwardIterator.
| ||
-ForwardIt1 must meet the requirements of LegacyForwardIterator.
| ||
-ForwardIt2 must meet the requirements of LegacyForwardIterator.
| ||
-BinaryPred must meet the requirements of BinaryPredicate.
| ||
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 \(\scriptsize N_1\)N1 as std::distance(first1, last1) and \(\scriptsize N_2\)N2 as std::distance(first2, last2):
operator==.p.operator==.p.Exceptions
- If the temporary memory resources required for parallelization are not available, std::bad_alloc is thrown.
- If an uncaught exception is thrown while accessing objects via an algorithm argument, the behavior is determined by the execution policy (for standard policies, std::terminate is invoked).
Possible implementation
| find_first_of (1) |
|---|
template<class InputIt, class ForwardIt>
InputIt find_first_of(InputIt first1, InputIt last1,
ForwardIt first2, ForwardIt last2)
{
for (; first1 != last1; ++first1)
for (ForwardIt it = first2; it != last2; ++it)
if (*first1 == *it)
return first1;
return last1;
}
|
| find_first_of (2) |
template<class InputIt, class ForwardIt, class BinaryPred>
InputIt find_first_of(InputIt first1, InputIt last1,
ForwardIt first2, ForwardIt last2,
BinaryPred p)
{
for (; first1 != last1; ++first1)
for (ForwardIt it = first2; it != last2; ++it)
if (p(*first1, *it))
return first1;
return last1;
}
|
Example
The following code searches for any of specified integers in a vector of integers:
#include <algorithm>
#include <iostream>
#include <vector>
auto print_sequence = [](const auto id, const auto& seq, int pos = -1)
{
std::cout << id << "{ ";
for (int i{}; const auto& e : seq)
{
const bool mark{i == pos};
std::cout << (i++ ? ", " : "");
std::cout << (mark ? "[ " : "") << e << (mark ? " ]" : "");
}
std::cout << " }\n";
};
int main()
{
const std::vector<int> v{0, 2, 3, 25, 5};
const auto t1 = {19, 10, 3, 4};
const auto t2 = {1, 6, 7, 9};
auto find_any_of = [](const auto& v, const auto& t)
{
const auto result = 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
{
const auto pos = 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 |
|---|---|---|---|
| LWG 576 | C++98 | first1 and last1 needed to be LegacyForwardIterators
|
they only need to be LegacyInputIterators |
| LWG 1205 | C++98 | the return value was unclear if the target range is empty | returns last1 in this case
|
See also
(C++20) |
searches for any one of a set of elements (algorithm function object) |
(C++11) |
finds the first element satisfying specific criteria (function template & algorithm function object) |
(C++20)(C++20)(C++20) |