Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm/find first of"

From cppreference.com
< cpp‎ | algorithm
m (Text replace - "===Equivalent function===" to "===Possible implementation===")
m (Text replace - "{{example cpp" to "{{example")
Line 66: Line 66:
  
 
===Example===
 
===Example===
{{example cpp
+
{{example
 
  | The following code searches for any of specified integers in a vector of integers:  
 
  | The following code searches for any of specified integers in a vector of integers:  
 
  | code=
 
  | code=

Revision as of 15:27, 19 April 2012

Template:cpp/algorithm/sidebar Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <algorithm>
</td>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td >
template< class ForwardIterator1, class ForwardIterator2 >

ForwardIterator1 find_first_of( ForwardIterator1 first, ForwardIterator1 last,

                                ForwardIterator2 s_first, ForwardIterator2 s_last );
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class ForwardIterator1, class ForwardIterator2, class BinaryPredicate >

ForwardIterator1 find_first_of( ForwardIterator1 first, ForwardIterator1 last,

                                ForwardIterator2 s_first, ForwardIterator2 s_last, BinaryPredicate p );
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Searches the range [first, last) for any of the elements in the range [s_first, s_last). The first version uses operator== to compare the elements, the second version uses the given binary predicate p.

Contents

Parameters

first, last - the range of elements to examine
s_first, s_last - the range of elements to search for
p - binary predicate which returns ​true if the elements should be treated as equal.

The signature of the predicate function should be equivalent to the following:

 bool pred(const Type1 &a, const Type2 &b);

While the signature does not need to have const &, the function must not modify the objects passed to it and must be able to accept all values of type (possibly const) Type1 and Type2 regardless of value category (thus, Type1 & is not allowed, nor is Type1 unless for Type1 a move is equivalent to a copy(since C++11)).
The types Type1 and Type2 must be such that objects of types ForwardIterator1 and ForwardIterator2 can be dereferenced and then implicitly converted to Type1 and Type2 respectively. ​

Return value

iterator to the first element in the range [first, last) that is equal to an element from the range [s_first; s_last). If no such element is found, last is returned.

Complexity

does at most (S*N) comparisons where Template:cpp and Template:cpp.

Possible implementation

Template:eq fun cpp

Example

The following code searches for any of specified integers in a vector of integers:

#include <algorithm>
#include <iostream>
#include <vector>
 
int main()
{
    std::vector<int> v{0, 2, 3, 25, 5};
    std::vector<int> t{3, 19, 10, 2};
 
    auto result = std::find_first_of(v.begin(), v.end(), t.begin(), t.end());
 
    if (result == v.end()) {
        std::cout << "no elements of v were equal to 3, 19, 10 or 2\n";
    } else {
        std::cout << "found a match at "
                  << std::distance(v.begin(), result) << "\n";
    }
 }

Output:

found a match at 1

See also

Template:cpp/algorithm/dcl list find