Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
STL Algorithms
algorithm:
adjacent_find
binary_search
copy
copy_backward
count
count_if
equal
equal_range
fill
fill_n
find
find_end
find_first_of
find_if
for_each
generate
generate_n
includes
inplace_merge
iter_swap
lexicographical_compare
lower_bound
make_heap
max
max_element
merge
min
min_element
mismatch
next_permutation
nth_element
partial_sort
partial_sort_copy
partition
pop_heap
prev_permutation
push_heap
random_shuffle
remove
remove_copy
remove_copy_if
remove_if
replace
replace_copy
replace_copy_if
replace_if
reverse
reverse_copy
rotate
rotate_copy
search
search_n
set_difference
set_intersection
set_symmetric_difference
set_union
sort
sort_heap
stable_partition
stable_sort
swap
swap_ranges
transform
unique
unique_copy
upper_bound


find_first_of

function template
<algorithm>
template <class ForwardIterator1, class ForwardIterator2>
   ForwardIterator1 find_first_of ( ForwardIterator1 first1, ForwardIterator1 last1,
                                    ForwardIterator2 first2, ForwardIterator2 last2 );

template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
   ForwardIterator1 find_first_of ( ForwardIterator1 first1, ForwardIterator1 last1,
                                    ForwardIterator2 first2, ForwardIterator2 last2,
                                    BinaryPredicate pred );

Find element from set in range

Returns an interator to the first occurrence in the range [first1,last1) of any of the elements in [first2,last2).

All the elements in [first2,last2) are compared to the each of the values in [first,last) until a match is found. The comparison is performed by either applying the == operator, or the template parameter comp (for the second version).

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
template<class ForwardIterator1, class ForwardIterator2>
  ForwardIterator1 find_first_of ( ForwardIterator1 first1, ForwardIterator1 last1,
                                   ForwardIterator2 first2, ForwardIterator2 last2)
{
  for ( ; first1 != last1; ++first1 )
    for (ForwardIterator2 it=first2; it!=last2; ++it)
      if (*it==*first1)          // or: if (comp(*it,*first)) for the pred version
        return first1;
  return last1;
}


Parameters

first1, last1
Forward iterators to the initial and final positions of the searched sequence. The range used is [first1,last1), which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1.
first2, last2
Forward iterators to the initial and final positions of the element values to be searched for. The range used is [first2,last2).
pred
Binary predicate taking two elements as argument, and returning the result of the comparison between them, with true (non-zero) meaning that they are to be considered equal, and false (zero) that they are not-equal. This can either be a pointer to a function or an object whose class overloads operator().

Return value

An iterator to the first element in [first1,last1) that is part of [first2,last2).
If none of the elements is found, the function returns last1.

Example

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
33
// find_first_of example
#include <iostream>
#include <algorithm>
#include <cctype>
#include <vector>
using namespace std;
bool comp_case_insensitive (char c1, char c2) {
  return (tolower(c1)==tolower(c2));
}
int main () {
  int mychars[] = {'a','b','c','A','B','C'};
  vector<char> myvector (mychars,mychars+6);
  vector<char>::iterator it;
  int match[] = {'A','B','C'};
  // using default comparison:
  it = find_first_of (myvector.begin(), myvector.end(), match, match+3);
  if (it!=myvector.end())
    cout << "first match is: " << *it << endl;
  // using predicate comparison:
  it = find_first_of (myvector.begin(), myvector.end(),
                      match, match+3, comp_case_insensitive);
  if (it!=myvector.end())
    cout << "first match is: " << *it << endl;
  
  return 0;
}


Output:
First match is: A
First match is: a

Complexity

At most, performs count1*count2 comparisons or applications of pred (where countX is the distance between firstX and lastX).

See also