Skip to content
Tutorials Center
My learning

Topics

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.

C++
auto it = std::max_element(v.begin(), v.end());
if (it != v.end()) { /* *it is the maximum, it - v.begin() its index */ }

Try it

C++ Finding extremes and their positions

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.

C++ By a key, with a comparator

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

C++
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_element still takes a "less than" comparator. Passing > to max_element therefore finds the smallest element, which is almost never what you want.
  • Ties: min_element and max_element return the first of equal candidates; minmax_element returns the last maximum. Matters for records that compare equal but differ in other fields.
  • Complexity: exactly max(N - 1, 0) comparisons for min_element and max_element; minmax_element uses at most 3(N - 1) / 2, fewer than two separate scans.
  • Only forward iterators needed, so it works on std::list, std::forward_list and others. For std::set and std::map use begin() and rbegin() instead (sorted by key).
  • Value, not position: if you only need the value of two items use std::min and std::max. std::ranges::min and std::ranges::max return the value of a whole range.
  • Index of the extreme: std::distance(v.begin(), it) or it - v.begin().
  • Top k: for several largest values use partial_sort or nth_element.