Standard C++ Library Class Reference
BinaryPredicate pred);
Description
The find_first_of algorithm finds a the first occurrence of a value from a sequence, specified by
first2, last2, in a sequence specified by first1, last1. The algorithm returns an iterator in the
range [first1, last1) that points to the first matching element. If the first sequence [first1, last1)
does not contain any of the values in the second sequence, find_first_of returns last1.
In other words, find_first_of returns the first iterator i in the [first1, last1)such that for some
integer j in the range [first2, last2):the following conditions hold:
*i == *j, pred(*i,*j) == true.
Or find_first_of returns last1 if no such iterator is found.
Two versions of the algorithm exist. The first uses the equality operator as the default binary
predicate, and the second allows you to specify a binary predicate.
Complexity
At most (last1 - first1)*(last2 - first2) applications of the corresponding predicate are done.
Example
//
// find_f_o.cpp
//
#include <vector>
#include <iterator>
#include <algorithm>
#include <iostream.h>
int main()
{
typedef vector<int>::iterator iterator;
int d1[10] = {0,1,2,2,3,4,2,2,6,7};
int d2[2] = {6,4};
//
// Set up two vectors
//
vector<int> v1(d1,d1 + 10), v2(d2,d2 + 2);
//
// Try both find_first_of variants
//
iterator it1 =
find_first_of(v1.begin(),v1.end(),v2.begin(),v2.end());
find_first_of(v1.begin(),v1.end(),v2.begin(),v2.end(),