std::find, std::find_if, std::find_if_not

From Cppreference

Jump to: navigation, search
Defined in header <algorithm>

template< class InputIterator, class T >

InputIterator find( InputIterator first, InputIterator last,

                    const T& value );
(1)
template< class InputIterator, class UnaryPredicate >

InputIterator find_if( InputIterator first, InputIterator last,

                       UnaryPredicate p );
(2)
template< class InputIterator, class UnaryPredicator >

InputIterator find_if_not( InputIterator first, InputIterator last,

                           UnaryPredicator q );
(3) (C++11 feature)

Finds the first element in the range [first, last) satisfying specific criteria. The first version searches for element, equal to value, the second version searches for element for which predicate p returns true, the third version searches for element for which predicate q returns false.

Contents

[edit] 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 signature of the predicate function should be equivalent to the following:

bool pred(const Type &a);

The signature does not need to have const &, but the function must not modify the objects passed to it.
The type ​Type​ must be such that an object of type ​InputIterator​ can be dereferenced and then implicitly converted to ​Type​. ​

q - unary predicate which returns ​false for the required element.

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

bool pred(const Type &a);

The signature does not need to have const &, but the function must not modify the objects passed to it.
The type ​Type​ must be such that an object of type ​InputIterator​ can be dereferenced and then implicitly converted to ​Type​. ​

[edit] Return value

iterator to the first element satisfying the condition or last if no such element is found.

[edit] Complexity

linear in the distance between first and last

[edit] Equivalent function

[edit] Example

The following code uses find to search a vector of integers for the number 3:

int num_to_find = 3;
 
std::vector<int> v1;
for (int i = 0; i < 10; i++) {
    v1.push_back(i);
}
 
std::vector<int>::iterator result;
result = find(v1.begin(), v1.end(), num_to_find);
 
if (result == v1.end()) {
    std::cout << "did not find a match for: " << num_to_find;
} else {
    std::cout << "match: " << *result;
}

Output:

​match: 3​

[edit] See also

adjacent_find
finds two identical (or some other relationship) items adjacent to each other
(function template)
find_end
finds the last sequence of elements in a certain range
(function template)
find_first_of
searches for any one of a set of elements
(function template)
mismatch
finds the first position where two ranges differ
(function template)
search
searches for a range of elements
(function template)
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox
In other languages