function template
<algorithm>
std::find_first_of equality (1)template <class ForwardIterator1, class ForwardIterator2> ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2);predicate (2)
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);equality (1)
template <class InputIterator, class ForwardIterator> InputIterator find_first_of (InputIterator first1, InputIterator last1, ForwardIterator first2, ForwardIterator last2);predicate (2)
template <class InputIterator, class ForwardIterator, class BinaryPredicate> InputIterator find_first_of (InputIterator first1, InputIterator last1, ForwardIterator first2, ForwardIterator last2, BinaryPredicate pred);
Find element from set in range
Returns an iterator to the first element in the range[first1,last1)
that matches any of the elements in [first2,last2)
. If no such element is found, the function returns last1.
The elements in [first1,last1)
are sequentially compared to each of the values in [first2,last2)
using operator==
(or pred, in version (2)), until a pair matches.
The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
12
13
template<class InputIterator, class ForwardIterator>
InputIterator find_first_of ( InputIterator first1, InputIterator last1,
ForwardIterator first2, ForwardIterator last2)
{
while (first1!=last1) {
for (ForwardIterator it=first2; it!=last2; ++it) {
if (*it==*first1) // or: if (pred(*it,*first)) for version (2)
return first1;
}
++first1;
}
return last1;
}
to the initial and final positions of the searched sequence. The range used is
[first1,last1)
, which contains all the elements between
first1and
last1, including the element pointed by
first1but not the element pointed by
last1.
to the initial and final positions of the searched sequence. The range used is
[first1,last1)
, which contains all the elements between
first1and
last1, including the element pointed by
first1but not the element pointed by
last1.
[first2,last2)
.
operator==
(with the elements of the first range as left-hand side operands, and those of the second as right-hand side operands).
bool
. The value returned indicates whether the elements are considered to match in the context of this function.
[first1,last1)
that is part of [first2,last2)
.
If [first2,last2)
is an empty range, the result is unspecified.
If [first2,last2)
is an empty range, the function returns last1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// find_first_of example
#include <iostream> // std::cout
#include <algorithm> // std::find_first_of
#include <vector> // std::vector
#include <cctype> // std::tolower
bool comp_case_insensitive (char c1, char c2) {
return (std::tolower(c1)==std::tolower(c2));
}
int main () {
int mychars[] = {'a','b','c','A','B','C'};
std::vector<char> haystack (mychars,mychars+6);
std::vector<char>::iterator it;
int needle[] = {'A','B','C'};
// using default comparison:
it = find_first_of (haystack.begin(), haystack.end(), needle, needle+3);
if (it!=haystack.end())
std::cout << "The first match is: " << *it << '\n';
// using predicate comparison:
it = find_first_of (haystack.begin(), haystack.end(),
needle, needle+3, comp_case_insensitive);
if (it!=haystack.end())
std::cout << "The first match is: " << *it << '\n';
return 0;
}
The first match is: A The first match is: a
count1*count2
(where countX is the distance between firstX and lastX): Compares elements until a match is found.
[first1,last1)
, and possibly more than once in [first2,last2)
).
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4