C++ Reference ยท Algorithms
The std::min_element and std::max_element algorithms
Reference for C++ min_element, max_element and minmax_element: find the smallest or largest element in a range, tie rules, comparators and empty ranges.
std::min_element and std::max_element scan a range and return an iterator to the smallest or largest element. Unlike std::min and std::max, which compare two values, these work on whole containers and tell you where the extreme is, not just what it is. std::minmax_element finds both in one pass and returns a pair of iterators.
If the range is empty, they return last, so you must check before dereferencing. For ties, the rules differ in a way that matters when equal elements are distinguishable: min_element returns the first smallest element, max_element the first largest, and minmax_element returns the first smallest and the last largest.
auto it = std::max_element(v.begin(), v.end()); if (it != v.end()) { /* *it is the maximum, it - v.begin() its index */ }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
template<class ForwardIt> ForwardIt min_element(ForwardIt first, ForwardIt last); template<class ForwardIt, class Compare> ForwardIt min_element(ForwardIt first, ForwardIt last, Compare comp); template<class ForwardIt> ForwardIt max_element(ForwardIt first, ForwardIt last); // and a Compare overload template<class ForwardIt> std::pair<ForwardIt, ForwardIt> minmax_element(ForwardIt first, ForwardIt last);
| Name | Description |
|---|---|
first, last |
The half-open range [first, last). |
comp |
Strict weak ordering: comp(a, b) is true when a is "less than" b. Default <. For max_element it is still the "less" function. |
Returns: an iterator to the extreme element, or last for an empty range. minmax_element returns a pair (min, max); for an empty range both are first.
Errors / undefined behavior: dereferencing the result when the range is empty is undefined behavior.
Usage notes
- Check emptiness first:
*std::max_element(v.begin(), v.end())on an empty vector is undefined behavior. - Comparator direction:
max_elementstill takes a "less than" comparator. Passing>tomax_elementtherefore finds the smallest element, which is almost never what you want. - Ties:
min_elementandmax_elementreturn the first of equal candidates;minmax_elementreturns the last maximum. Matters for records that compare equal but differ in other fields. - Complexity: exactly
max(N - 1, 0)comparisons formin_elementandmax_element;minmax_elementuses at most3(N - 1) / 2, fewer than two separate scans. - Only forward iterators needed, so it works on
std::list,std::forward_listand others. Forstd::setandstd::mapusebegin()andrbegin()instead (sorted by key). - Value, not position: if you only need the value of two items use
std::minandstd::max.std::ranges::minandstd::ranges::maxreturn the value of a whole range. - Index of the extreme:
std::distance(v.begin(), it)orit - v.begin(). - Top k: for several largest values use
partial_sortornth_element.