Apache C++ Standard Library Reference Guide

## find_first_of()

Library:  Algorithms

`Function`

No Entries

### Summary

Algorithm that finds the first occurrence of any value from one sequence in another sequence

### Synopsis

```#include <algorithm>

namespace std {
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 find_first_of (ForwardIterator1 start1,
ForwardIterator1 finish1,
ForwardIterator2 start2,
ForwardIterator2 finish2);

template <class ForwardIterator1, class ForwardIterator2,
class BinaryPredicate>
ForwardIterator1 find_first_of (ForwardIterator1 start1,
ForwardIterator1 finish1,
ForwardIterator2 start2,
ForwardIterator2 finish2,
BinaryPredicate binary_pred);
}
```

### Description

The find_first_of() algorithm finds the first occurrence of a value from a sequence, specified by start2, finish2, in a sequence specified by start1, finish1. The algorithm returns an iterator in the range [start1, finish1) that points to the first matching element. If the first sequence [start1, finish1) does not contain any of the values in the second sequence, find_first_of() returns finish1.

In other words, find_first_of() returns the first iterator i in [start1, finish1)such that for some iterator j in the range [start2, finish2), the following conditions hold:

```*i == *j, binary_pred(*i,*j) == true.
```

Or find_first_of() returns finish1 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 (finish1 - start1)*(finish2 - start2) applications of the corresponding predicate are done.

### Example

```//
// find_f_o.cpp
//

#include <algorithm>    // for find_first_of
#include <functional>   // for equal_to
#include <iostream>     // for cout, endl
#include <vector>       // for vector

int main ()
{
// Typedef for convenience.
typedef std::vector<int, std::allocator<int> > vector;

const vector::value_type a1[] = {
0, 1, 2, 2, 3, 4, 2, 2, 6, 7
};
const vector::value_type a2[] = { 6, 4 };

// Set up two vectors.
const vector v1(a1, a1 + sizeof a1 / sizeof *a1);
const vector v2(a2, a2 + sizeof a2 / sizeof *a2);

// Try both find_first_of variants.
vector::const_iterator it1 =
std::find_first_of(v1.begin(), v1.end(),
v2.begin(), v2.end());

vector::const_iterator it2 =
std::find_first_of(v1.begin(), v1.end(),
v2.begin(), v2.end(),
std::equal_to<vector::value_type>());

// Create an output stream iterator.
std::ostream_iterator<vector::value_type, char,
std::char_traits<char> > out(std::cout, " " );

// Output results.
std::cout << "For the vectors { ";
std::copy(v1.begin(), v1.end(), out);

std:: cout << "} and { ";
std::copy(v2.begin(), v2.end(), out);

std::cout << "}\nboth versions of find_first_of "
<< "point to: " << *it1 << std::endl;

// Return 0 on success, non-0 on failure.
return !(*it1 == *it2);
}

Program Output
```
```For the vectors { 0 1 2 2 3 4 2 2 6 7 } and { 6 4 }
both versions of find_first_of point to: 4
```

### Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 25.1.4