Open In App

std::min in C++

Last Updated : 19 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The std::min() is used to find the minimum element among the given elements. It is the built-in function of C++ STL defined inside <algorithm> header file.

Let's take the simplest example to demonstrate how the min() function works:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    cout << min(3,7);
    return 0;
}

Output
3

min() Function Signature

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

template< class T, class Compare >
T min (std::initializer_list<T> ilist, Compare comp);

The std::min() function can be used in three different ways to:

Find Minimum Among Two Elements

We can use std::min() function to find the smaller elements between the two elements. It uses < operator for comparison.

C++
min(a , b);

Here,

  • a: First value
  • b: Second value

This function returns the smaller of the two values. If both are equal, returns the first value.

Example

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    int a = 8, b = 91;

    // Finding minimum among a and b
    cout << min(a, b);
    return 0;
}

Output
8

Time complexity: O(1)
Auxiliary Space: O(1)

Find the Minimum Among the Multiple Values

The std::min() function can also find the minimum value between more than two values. To achieve this, we have to pass the values in an initializer list, enclosed in {} and separated by commas. It uses < operator with to compare all the values pairwise.

C++
min({v1, v2, v3...});

Here,

  • v1, v2, v3...: List of values.

This function returns the smallest value among the given lists. If all are equal, return the first value.

Example

C++
#include<bits/stdc++.h> 
using namespace std; 

int main() { 
  
  // Finding the smallest of 
  // all the numbers 
  cout << min({1, 2, 3, 4, 5, 
               10, -1, 7}); 
  
  return 0; 
}

Output
-1

Find Minimum Using Custom Comparator

The std::min() function also supports the use of custom comparator function to change the way of comparison. It is by default set to find the minimum element but we can also change it to perform any other desired comparison. It is especially useful to compare values of user defined data type.

C++
std::min (a, b, comp)
std::min ({v1, v2, v3...}, comp)

where, comp is the comparator function, lambda expression or even functors. This comparator function should follow these rules:

  • Return value should be of bool or any bool convertible type.
  • Should take two arguments.
  • Should not modify the arguments.

It returns true if a is smaller than b. False otherwise.

Example

C++
#include <bits/stdc++.h>
using namespace std;

class St {
  public:
  int sno;
  string name;
  St(int val, string s): sno(val), name(s) {}
};

// Comparator that works 
// for data type A
bool comp(const St& a, const St& b) {
  return a.sno < b.sno;
}

int main() {
    St a(8, "Divesh");
  	St b(91, "Rohan");

    // std::min() with custom comparator
  	auto smaller = min(a, b, comp);
    cout << smaller.sno << " " 
         << smaller.name;
    return 0;
}

Output
8 Divesh

Next Article
Practice Tags :

Similar Reads