C++ std::min() Function

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

Definition

In C++, std::min() compares values and returns the smallest one. You can give it two values or a braced list of values.

A custom comparison function lets you choose what "smaller" means, for example the smaller absolute value.

Syntax

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

C++
std::min(a, b);
std::min({4, 9, 2, 7});
CallWhat it does
std::min(a, b);The smaller of two values.
std::min({4, 9, 2, 7});The smallest value in a list.
Library declarations
C++
template<class T>
const T& min(const T& a, const T& b);

template<class T>
T min(std::initializer_list<T> list);

template<class T, class Compare>
const T& min(const T& a, const T& b, Compare comp);

Parameters

ParameterDescription
a, bRequired (two-value form). The values to compare.
listA braced list such as {4, 9, 2}. It must not be empty.
compOptional. A comparison function that returns true when its first argument is smaller.

Return Value

Return ValueDescription
ReferenceThe two-value form returns a reference to the smaller argument. If they are equal, it returns the first.
ValueThe list form returns a copy of the smallest value.

C++ Examples

Example 1 — Two numbers

Pick the smaller of two numbers.

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

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

    int smaller = std::min(a, b);

    std::cout << "Smaller: " << smaller << '\n';
    return 0;
}

How It Works

  1. min(a, b) compares the two values with <.
  2. 3 is less than 8, so it is returned.
  3. The result is copied into smaller, so it is safe to keep.

Example 2 — Smallest in a list

Pass many values in braces.

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

int main() {
    int smallest = std::min({4, 9, 2, 7});

    std::cout << "Smallest: " << smallest << '\n';
    return 0;
}

How It Works

  1. The braces create an initializer list of four values.
  2. The list form checks all of them.
  3. It returns 2 as a copy.

Example 3 — Custom rule

Choose the number with the smaller absolute value.

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

int main() {
    int m = std::min({-5, 3, -1, 4}, [](int a, int b) {
        return std::abs(a) < std::abs(b);
    });

    std::cout << "Closest to zero: " << m << '\n';
    return 0;
}

How It Works

  1. The lambda compares the size of each number, ignoring the sign.
  2. The absolute values are 5, 3, 1 and 4, and 1 is the smallest.
  3. That belongs to -1, so -1 is returned.

Frequently Asked Questions

In C++, std::min() returns the smaller of two values, or the smallest in a list. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns the smaller of the two values (a reference), or the smallest value of an initializer list. If values are equal, the first one is returned.
It returns the first argument.
std::min() compares values you pass in. std::min_element() searches a range and returns an iterator.

Next: min_element()

Finds the smallest element in a range.

min_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.

10 people found this page helpful