SGI Logo


find_first_of

Category: algorithms Component type: function

Prototype

find_first_of is an overloaded name; there are actually two find_first_of functions.
template <class InputIterator, class ForwardIterator>
InputIterator find_first_of(InputIterator first1, InputIterator last1,
                            ForwardIterator first2, ForwardIterator last2);

template <class InputIterator, class ForwardIterator, class BinaryPredicate>
InputIterator find_first_of(InputIterator first1, InputIterator last1,
                            ForwardIterator first2, ForwardIterator last2,
                            BinaryPredicate comp);

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. Specifically, find_first_of searches for the first occurrance in the range [first1, last1) of any of the elements in [first2, last2). (Note that this behavior is reminiscent of the function strpbrk from the standard C library.)

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. 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.

Definition

Defined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.

Requirements on types

For the first version: For the second version:

Preconditions

Complexity

At most (last1 - first1) * (last2 - first2) comparisons.

Example

Like strpbrk, one use for find_first_of is finding whitespace in a string; space, tab, and newline are all whitespace characters.
int main()
{
  const char* WS = "\t\n ";
  const int n_WS = strlen(WS);

  char* s1 = "This sentence contains five words.";
  char* s2 = "OneWord";


  char* end1 = find_first_of(s1, s1 + strlen(s1),
                             WS, WS + n_WS); 
  char* end2 = find_first_of(s2, s2 + strlen(s2),
                             WS, WS + n_WS); 

  printf("First word of s1: %.*s\n", end1 - s1, s1); 
  printf("First word of s2: %.*s\n", end2 - s2, s2); 
}

Notes

See also

find, find_if, search


STL Main Page

Contact Us | Site Map | Trademarks | Privacy | Using this site means you accept its Terms of Use
Copyright © 2009 - 2014 Silicon Graphics International. All rights reserved.