Difference between revisions of "cpp/algorithm/find first of"
m (Text replace - "===Equivalent function===" to "===Possible implementation===") |
m (Text replace - "{{example cpp" to "{{example") |
||
Line 66: | Line 66: | ||
===Example=== | ===Example=== | ||
− | {{example | + | {{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><algorithm>
<td></td> <td></td> </tr> <tr class="t-dcl ">
<td >ForwardIterator1 find_first_of( ForwardIterator1 first, ForwardIterator1 last,
<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">
<td >ForwardIterator1 find_first_of( ForwardIterator1 first, ForwardIterator1 last,
<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) |
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
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