Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Algorithms

The std::min, std::max, std::minmax and std::clamp functions

Reference for C++ std::min, std::max, std::minmax and std::clamp: compare values, initializer lists, dangling references, comparators and the ranges versions.

std::min and std::max return the smaller or larger of two values. std::minmax returns both at once as a pair, and std::clamp (C++17) limits a value to a closed interval [low, high]. All four live in <algorithm> and work with any type that has operator< or that you give a comparator for. min, max and minmax also accept a braced list of values.

They return references to the arguments, not copies. That is efficient, but it creates one of the best-known traps in C++: binding the result to a reference when an argument was a temporary.

C++
int a = std::max(3, 8);           // 8
int c = std::clamp(15, 0, 10);    // 10

Try it

C++ min, max, minmax and clamp

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++ Comparators, ties and the dangling-reference trap

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 T> const T& min(const T& a, const T& b);
template<class T, class Compare> const T& min(const T& a, const T& b, Compare comp);
template<class T> T min(std::initializer_list<T> list);

template<class T> const T& max(const T& a, const T& b);          // same overload set as min
template<class T> std::pair<const T&, const T&> minmax(const T& a, const T& b);
template<class T> std::pair<T, T> minmax(std::initializer_list<T> list);

template<class T> constexpr const T& clamp(const T& v, const T& lo, const T& hi);   // C++17
template<class T, class Compare> constexpr const T& clamp(const T& v, const T& lo, const T& hi, Compare comp);
Name Description
a, b The values to compare. Both must be the same type T.
list A braced list such as {1, 5, 3}; it must not be empty.
v, lo, hi Value and the inclusive limits for clamp.
comp Strict weak ordering; the default is <.

Returns: a reference to the selected argument (min, max, clamp), a pair of references (minmax with two arguments), or values (the initializer-list forms). If the arguments are equivalent, min returns the first, max returns the first too, and minmax(a, b) returns (a, b). For the list forms, min returns the leftmost smallest and max the leftmost largest.

Errors / undefined behavior: clamp with hi less than lo is undefined. An empty initializer list is undefined. Using a returned reference after the argument ended its life is undefined.

Usage notes

  • Dangling references: the result refers to one of the arguments. If an argument is a temporary, it is destroyed at the end of the full expression, so const auto& s = std::max(std::string("a"), std::string("b")); leaves s dangling. The same goes for the references inside the pair from minmax(a, b). Store results in plain variables (auto s = ...) to get copies.
  • Same type needed: std::min(1, 2.5) does not compile. Cast one argument or write std::min<double>(1, 2.5).
  • Not the macro: in headers like <windows.h>, min and max can be macros; define NOMINMAX or write (std::min)(a, b).
  • NaN: with floating-point NaN the comparison is false in both directions, so the result depends on argument order. Avoid NaN inputs.
  • Ranges of values: for a container use min_element and max_element; std::ranges::min and std::ranges::max (C++20) accept a whole range.
  • Complexity: one comparison for two arguments, n - 1 for lists (minmax of a list uses at most 3n/2).
  • constexpr: all of these are usable in constant expressions since C++14 (clamp since C++17).
  • Integers and sign: comparing a signed and an unsigned value silently converts; use std::cmp_less (C++20) when the mix is intended.