std::find, std::find_if, std::find_if_not
From Cppreference
Defined in header
<algorithm> | ||
template< class InputIterator, class T >
InputIterator find( InputIterator first, InputIterator last, | (1) | |
template< class InputIterator, class UnaryPredicate >
InputIterator find_if( InputIterator first, InputIterator last, | (2) | |
template< class InputIterator, class UnaryPredicator >
InputIterator find_if_not( InputIterator first, InputIterator last, | (3) | (C++0x 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 |
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:
The signature does not need to have const &, but the function must not modify the objects passed to it. | |||||||||
q | - | unary predicate which returns false for the required element. The signature of the predicate function should be equivalent to the following:
The signature does not need to have const &, but the function must not modify the objects passed to it. |
Return value
iterator to the first element satisfying the condition or last if no such element is found.
Complexity
linear in the distance between first and last
Equivalent function
First version: |
---|
template<class InputIterator, class T> InputIterator find(InputIterator first, InputIterator last, const T& value) { for (; first != last; ++first) { if (*first == value) { return first; } } return last; } |
Second version: |
template< class InputIterator, class UnaryPredicate > InputIterator find(InputIterator first, InputIterator last, UnaryPredicate p) { for (; first != last; ++first) { if (p(*first)) return first; } return last; } |
Third version: |
template< class InputIterator, class BinaryPredicator > InputIterator find(InputIterator first, InputIterator last, BinaryPredicator p) { for (; first != last; ++first) { if (!p(*first)) return first; } return last; } |
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
See also
| finds two identical (or some other relationship) items adjacent to each other (function template) | |
| finds the last sequence of elements in a certain range (function template) | |
| searches for any one of a set of elements (function template) | |
| finds the first position where two ranges differ (function template) | |
| searches for a range of elements (function template) |