Вы находитесь на странице: 1из 3

std::nd, std::nd_if, std::nd_if_not

Dened in header <algorithm>

template< class InputIt, class T >


InputIt find( InputIt first, InputIt last, const T& value );

(1)

template< class InputIt, class UnaryPredicate >


InputIt find_if( InputIt first, InputIt last,
UnaryPredicate p );

(2)

template< class InputIt, class UnaryPredicate >


InputIt find_if_not( InputIt first, InputIt last,
UnaryPredicate q );

(3)

(sinc e C++11)

Returns the rs t element in the range [first, last)that s atis es s pecic criteria:
1) finds earches for an element equal to value
2) find_ifs earches for an element for which predicate preturns true
3) find_if_nots earches for an element for which predicate qreturns false

Parameters
first, last - the range of elements to examine
value - value to compare the elements to
p - unary predicate which returns true for the required element.
The s ignature of the predicate function s hould be equivalent to the following:
bool pred(const Type &a);
The s ignature does not need to have const &, but the function mus t not modify the
objects pas s ed to it.
The type Type mus t be s uch that an object of type InputIt can be dereferenced
and then implicitly converted to Type.
q - unary predicate which returns false for the required element.
The s ignature of the predicate function s hould be equivalent to the following:
bool pred(const Type &a);
The s ignature does not need to have const &, but the function mus t not modify the
objects pas s ed to it.
The type Type mus t be s uch that an object of type InputIt can be dereferenced
and then implicitly converted to Type.
Type requirements
-

InputItmus t meet the requirements of InputIterator.


UnaryPredicatemus t meet the requirements of Predicate.

Return value
Iterator to the rs t element s atis fying the condition or lastif no s uch element is found.

Complexity
At mos t last- firstapplications of the predicate

Possible implementation
First version

template<class InputIt, class T>


InputIt find(InputIt first, InputIt last, const T& value)
{
for (; first != last; ++first) {
if (*first == value) {
return first;
}
}
return last;
}
Second version
template<class InputIt, class UnaryPredicate>
InputIt find_if(InputIt first, InputIt last, UnaryPredicate p)
{
for (; first != last; ++first) {
if (p(*first)) {
return first;
}
}
return last;
}
Third version
template<class InputIt, class UnaryPredicate>
InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q)
{
for (; first != last; ++first) {
if (!q(*first)) {
return first;
}
}
return last;
}

If you do not have C++11, an equivalent to std::find_if_notis to us e std::find_ifwith the negated


predicate.

template<class InputIt, class UnaryPredicate>


InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q)
{
return std::find_if(first, last, std::not1(q));
}

Example
The following example nds an integer in a vector of integers .
Run this code

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>

int main()
{
int n1 = 3;
int n2 = 5;

std::vector<int> v{0, 1, 2, 3, 4};

auto result1 = std::find(std::begin(v), std::end(v), n1);


auto result2 = std::find(std::begin(v), std::end(v), n2);
if (result1 != std::end(v)) {
std::cout << "v contains: " << n1 << '\n';
} else {
std::cout << "v does not contain: " << n1 << '\n';
}
if (result2 != std::end(v)) {
std::cout << "v contains: " << n2 << '\n';
} else {
std::cout << "v does not contain: " << n2 << '\n';
}

Output:
v contains: 3
v does not contain: 5

See also
adjacent_find

nds the rs t two adjacent items that


are equal (or s atis fy a given predicate)
(func tion template)

find_end

nds the las t s equence of elements in a


certain range
(func tion template)

find_first_of

s earches for any one of a s et of


elements
(func tion template)

mismatch

nds the rs t pos ition where two ranges


dier
(func tion template)

search
std::experimental::parallel::find(parallelism TS )
std::experimental::parallel::find_if(parallelism TS )

s earches for a range of elements


(func tion template)

parallelized vers ion of std::find


(func tion template)

parallelized vers ion of std::find_if


(func tion template)

parallelized vers ion of


std::experimental::parallel::find_if_not(parallelism TS ) std::find_if_not
(func tion template)
Retrieved from "http://en.c ppreferenc e.c om/mwiki/index.php?title=c pp/algorithm/nd& oldid=79876"

Вам также может понравиться