C++ std::minmax() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++11

Definition

In C++, std::minmax() returns both the smallest and the largest value in one call, as a std::pair. The first member is the minimum and the second is the maximum.

It is a handy shortcut when you need both values.

Syntax

Add #include <algorithm>, then call std::minmax(). Start with these two lines.

C++
auto r = std::minmax(a, b);
auto r = std::minmax({5, 1, 9, 3});
CallWhat it does
auto r = std::minmax(a, b);Get r.first (smaller) and r.second (larger).
auto r = std::minmax({5, 1, 9, 3});Get both ends of a list.
Library declarations
C++
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);

Parameters

ParameterDescription
a, bRequired (two-value form). The values to compare.
listA braced list. It must not be empty.
compOptional. A comparison function.

Return Value

Return ValueDescription
std::pairfirst is the smallest value and second is the largest.

C++ Examples

Example 1 — Two values

Get both values in order.

C++
#include <algorithm>
#include <iostream>

int main() {
    int a = 3;
    int b = 8;

    auto r = std::minmax(a, b);

    std::cout << "min=" << r.first << " max=" << r.second << '\n';
    return 0;
}

How It Works

  1. minmax() returns a pair of references to a and b.
  2. r.first refers to the smaller one, 3.
  3. r.second refers to the larger one, 8.

Example 2 — A list

Find the smallest and largest in one call.

C++
#include <algorithm>
#include <iostream>

int main() {
    auto r = std::minmax({5, 1, 9, 3});

    std::cout << "min=" << r.first << " max=" << r.second << '\n';
    return 0;
}

How It Works

  1. The list form returns a pair of values, not references.
  2. It looks at all four numbers.
  3. 1 is the smallest and 9 is the largest.

Example 3 — Argument order does not matter

Swap the arguments and get the same pair.

C++
#include <algorithm>
#include <iostream>

int main() {
    int x = 10;
    int y = 2;

    auto r = std::minmax(x, y);

    std::cout << "smaller: " << r.first << '\n';
    std::cout << "larger: " << r.second << '\n';
    return 0;
}

How It Works

  1. Here the larger value 10 is passed first.
  2. minmax() still puts the smaller one in first.
  3. You never have to sort the two values yourself.

Frequently Asked Questions

In C++, std::minmax() returns the smaller and the larger of two values (or of a list) at once. Include <algorithm> to use it.
Include <algorithm>. It needs C++11 or newer, so compile with -std=c++11 or later.
It returns a std::pair where first is the smallest value and second is the largest value.
A std::pair. The first member is the smallest value and the second is the largest.
For a list it makes fewer comparisons than calling both functions separately.

Next: minmax_element()

Finds the smallest and the largest elements of a range in one pass.

minmax_element() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful