Defined in header <algorithm> | |||
---|---|---|---|
(1) | |||
| (until C++17) | ||
| (since C++17) | ||
(2) | |||
| (until C++17) | ||
| (since C++17) |
Finds the smallest element in the range [first, last)
. The first version uses operator<
to compare the values, the second version uses the given comparison function comp
.
Parameters
first, last | - | forward iterators defining the range to examine |
cmp | - | comparison function object (i.e. an object that satisfies the requirements of Compare ) which returns true if a is less than b . The signature of the comparison function should be equivalent to the following:
The signature does not need to have |
Type requirements | ||
- ForwardIt must meet the requirements of ForwardIterator . |
Return value
Iterator to the smallest element in the range [first, last)
. If several elements in the range are equivalent to the smallest element, returns the iterator to the first such element. Returns last
if the range is empty.
Complexity
Exactly max(N-1,0) comparisons, where N = std::distance(first, last)
.
Possible implementation
First version | ||
---|---|---|
| ||
Second version | ||
|
Example
1 2 3 4 5 6 7 8 9 10 11 | #include <algorithm> #include <iostream> #include <vector> int main() { std::vector< int > v{3, 1, 4, 1, 5, 9}; std::vector< int >::iterator result = std::min_element(std::begin(v), std::end(v)); std::cout << "min element at: " << std::distance(std::begin(v), result); } |
Output:
1 | min element at: 1 |
See also
returns the largest element in a range (function template) | |
(C++11) | returns the smallest and the largest element in a range (function template) |
returns the smaller of the given values (function template) | |
std::experimental::parallel::min_element
(parallelism TS) | parallelized version of std::min_element (function template) |
Please login to continue.