std::minmax_element
| Defined in header <algorithm>
|
||
template< class ForwardIt >
std::pair<ForwardIt, ForwardIt>
minmax_element( ForwardIt first, ForwardIt last );
|
(1) | (since C++11) (constexpr since C++17) |
template< class ForwardIt, class Compare >
std::pair<ForwardIt, ForwardIt>
minmax_element( ForwardIt first, ForwardIt last, Compare comp );
|
(2) | (since C++11) (constexpr since C++17) |
template< class ExecutionPolicy, class ForwardIt >
std::pair<ForwardIt, ForwardIt>
minmax_element( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last );
|
(3) | (since C++17) |
template< class ExecutionPolicy, class ForwardIt, class Compare >
std::pair<ForwardIt, ForwardIt>
minmax_element( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, Compare comp );
|
(4) | (since C++17) |
Finds the smallest and greatest element in the target range [first, last).
operator<(until C++20)std::less{}(since C++20).comp.policy.true:
|
|
(until C++20) |
|
|
(since C++20) |
Parameters
| first, last | - | the pair of iterators defining the target range |
| comp | - | comparison function object (i.e. an object that satisfies the requirements of Compare) which returns true if the first argument is less than the second.The signature of the comparison function should be equivalent to the following:
While the signature does not need to have |
| policy | - | the execution policy to use |
| Type requirements | ||
-ForwardIt must meet the requirements of LegacyForwardIterator.
| ||
Return value
A std::pair object, where:
- The data member
firstholds the iterator to the smallest element in the target range.- If there are multiple smallest values,
firstholds the iterator to the first one of them. - If the target range is empty,
firstholdslast.
- If there are multiple smallest values,
- The data member
secondholds the iterator to the greatest element in the target range.- If there are multiple greatest values,
secondholds the iterator to the last one of them. - If the target range is empty,
secondholdslast.
- If there are multiple greatest values,
Complexity
Given N as std::distance(first, last):
| 3 |
| 2 |
operator<(until C++20)std::less{}(since C++20).| 3 |
| 2 |
comp.operator<(until C++20)std::less{}(since C++20).comp.Exceptions
- If the temporary memory resources required for parallelization are not available, std::bad_alloc is thrown.
- If an uncaught exception is thrown while accessing objects via an algorithm argument, the behavior is determined by the execution policy (for standard policies, std::terminate is invoked).
Notes
This algorithm is different from std::make_pair(std::min_element(), std::max_element()), not only in efficiency, but also in that this algorithm finds the last biggest element while std::max_element finds the first biggest element.
Possible implementation
| minmax_element (1) |
|---|
template<class ForwardIt>
std::pair<ForwardIt, ForwardIt>
minmax_element(ForwardIt first, ForwardIt last)
{
using value_type = typename std::iterator_traits<ForwardIt>::value_type;
return std::minmax_element(first, last, std::less<value_type>());
}
|
| minmax_element (2) |
template<class ForwardIt, class Compare>
std::pair<ForwardIt, ForwardIt>
minmax_element(ForwardIt first, ForwardIt last, Compare comp)
{
auto min = first, max = first;
if (first == last || ++first == last)
return {min, max};
if (comp(*first, *min))
min = first;
else
max = first;
while (++first != last)
{
auto i = first;
if (++first == last)
{
if (comp(*i, *min))
min = i;
else if (!(comp(*i, *max)))
max = i;
break;
}
else
{
if (comp(*first, *i))
{
if (comp(*first, *min))
min = first;
if (!(comp(*i, *max)))
max = i;
}
else
{
if (comp(*i, *min))
min = i;
if (!(comp(*first, *max)))
max = first;
}
}
}
return {min, max};
}
|
Example
#include <algorithm>
#include <iostream>
int main()
{
const auto v = {3, 9, 1, 4, 2, 5, 9};
const auto [min, max] = std::minmax_element(begin(v), end(v));
std::cout << "min = " << *min << ", max = " << *max << '\n';
}
Output:
min = 1, max = 9
See also
(C++20) |
returns the smallest and the largest elements in a range (algorithm function object) |
| returns the smallest element in a range (function template & algorithm function object) | |
(C++20) |
|
| returns the largest element in a range (function template & algorithm function object) | |
(C++20) |