# find_first_of

Searches the first of several possible elements

## Definition

The**find_first_of()**algorithm is defined in the standard header

**<algorithm>**and in the nonstandard backward-compatibility header

**<algo.h>**.

## Interface

#include <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 comp );

**Parameters:**

Parameter | Description |
---|---|

first1 | A forward iterator addressing the position of the first element in the range to be searched |

last1 | A forward iterator addressing the position one past the final element in the range to be searched |

first2 | A forward iterator addressing the position of the first element in the range to be matched |

last2 | A forward iterator addressing the position one past the final element in the range to be matched |

comp | User-defined predicate function object that defines the condition to be satisfied if two elements are to be taken as equivalent. A binary predicate takes two arguments and returns true when satisfied and false when not satisfied |

## Description

**Find_first_of**is similar to find, in that it performs linear search through a range of Input Iterators. The difference is that while find searches for one particular value, find_first_of searches for

**any**of several values. The two versions of find_first_of differ in how they compare elements for equality. The first uses

**operator==**and the second uses and arbitrary user-supplied function object

`comp`

.## Return Value

The first version returns the first iterator`i`

in `[first1, last1)`

such that, for some iterator `j`

in `[first2, last2)`

, `*i == *j`

.
The second returns the first iterator `i`

in `[first1, last1)`

such that, for some iterator `j`

in `[first2, last2)`

, `comp(*i, *j)`

is true. As usual, both versions return `last1`

if no such iterator `i`

exists.## Complexity

At most, performs`distance1*distance2`

comparisons or applications of `comp`

(where `distanceX`

is the distance between `firstX`

and `lastX`

).### References

Example:

##### Example - find_first_of algorithm

Problem

This program illustrates the use of the STL

**find_first_of()**algorithm (default version) to find the first occurrence of any one of a range of integer values in a vector within another range of integer values, also in a vector.Workings

#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int a1[] = {1, 2, 333, 3, 4, 5, 6, 444, 7, 8, 9, 10}; vector<int> v1(a1, a1+12); cout <<"\nHere are the contents of v1:\n"; for (vector<int>::size_type i=0; i<v1.size(); i++) cout <<v1.at(i)<<" "; int a2[] = {333, 444, 555}; vector<int> v2(a2, a2+3); cout <<"\nHere are the contents of v2:\n"; for (vector<int>::size_type i=0; i<v2.size(); i++) cout <<v2.at(i)<<" "; vector<int>::iterator p; p = find_first_of(v1.begin(), v1.end(), v2.begin(), v2.end()); if (p != v1.end()) cout <<"\nThe first instance of a value from v2 occurring in v1 happens at location " <<(int)(p-v1.begin()+1)<<"."; else cout <<"\nNo instance of v2 was found in v1."; p = find_first_of(p+1, v1.end(), v2.begin(), v2.end()); if (p != v1.end()) cout <<"\nThe next instance of a value from v2 occurring in v1 happens at location " <<(int)(p-v1.begin()+1) << "."; else cout <<"\nNo further instance of a value from v2 occurring in v1 was found."; p = find_first_of(p+1, v1.end(), v2.begin(), v2.end()); if (p != v1.end()) cout <<"\nThe next instance of a value from v2 occurring in v1 happens at location " <<(int)(p-v1.begin()+1)<<"."; else cout <<"\nNo further instance of a value from v2 occurring in v1 was found."; return 0; }

Solution

**Output:**Here are the contents of v1:

1 2 333 3 4 5 6 444 7 8 9 10 Here are the contents of v2:

333 444 555 The first instance of a value from v2 occurring in v1 happens at location 3. The next instance of a value from v2 occurring in v1 happens at location 8. No further instance of a value from v2 occurring in v1 was found.

References