VOOZH about

URL: https://www.geeksforgeeks.org/cpp/stdmin-in-cpp/

⇱ std::min in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

std::min in C++

Last Updated : 19 May, 2025

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:


Output
3

min() Function Signature

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.

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


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.

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


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.

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


Output
8 Divesh
Comment
Article Tags: