std::min in C++

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The min function in C++ is used to compare among given elements; either the elements could be two or more than 2, and the parameters vary for different overloads. Subsequently, the algorithm finds the minimum of them. It is declared in the header of C++. In this function, we can also use the custom compare function to decide what we consider as a minimum.

Syntax of std::min in C++

The syntax of the min function might look weird to beginners, but in this article, we will discuss it in detail to clarify the concepts. If syntax seems complicated, you should visit the first example to get a good idea in simple terms.

There exist several overloads to this function; the syntax of four major overloads is given here,

Default

Custom Compare Function

Compare function accepts two parameters and returns a boolean value(true if the first argument is greater or equal to the second and false otherwise). The execution of a function intends to find which one of the provided parameters is minimum or maximum. The person writing the compare function definition can decide what should be considered as a minimum or maximum.

Initializer List

An initializer list is a way to access several elements of the same type. From a beginner's perspective, you can consider this an array or vector with different properties.

Parameters of std::min in C++

It accepts two or more (in the case of the initializer list) values to compare. We can provide a custom compare function.

There exist two scenarios of passing parameters.

Two Variables

We can pass two variables, a and b, of any inbuilt or user-defined type.

One important point to notice is that the std::min function only accepts the variable's reference with the const keyword, which means the function can only read the variable. Hence our values are safe during the execution of the function.

Initializer List

An Initializer list is an object of type std::initializer_list<T> that provides access to an array of objects of type const T.

Because the std::initializer list has a defined size and doesn't need dynamic allocation, it can be implemented efficiently. The initializer list's primary function is initializing an object, it uses the optimal storage location and avoids unnecessary overheads.

In simple terms, it is just a collection of elements of the same data type. So, we can also pass this initializer list to the min function in c++ as a parameter.

Return Value of std::min in C++

Returns the minimum value after comparison with the same data type provided as a parameter.

There exist two scenarios of passing parameters hence the return value will also be determined according to the technique of passing the parameters,

Two Variables

If two variables, a, and b, are provided, the smaller value between a and b is returned. In the case of equivalent values, it returns a or can, say, the first parameter.

Initializer List

The element with the smallest value among several elements of the initializer list is returned. The leftmost minimum value will be returned if more than one value is also the minimum.

How Does std::min in C++ Work?

The algorithm header of the Standard Template Library contains the declaration of several algorithmic and mathematical functions; the min function is also one of them. It is used to find the minimum of the provided parameter.

The min function accepts the const reference of two variables and then returns the const reference of the same type. It can work with inbuilt and custom data types because of generic programming support in the function.

Apart from that, we can always provide the optional parameter as a comparator function which we can use to determine the minimum of the two given parameters.

Examples

Basic Comparison

Output:

Using Initializer List

We can provide elements in the initializer list with elements wrapped inside curly braces {} and separated by commas ,.

Output:

Conclusion

  • min function in C++ is used to find the minimum of provided parameters,
  • The parameters could be of any type, either inbuilt or user-defined. There is full support of Generic Programming.
  • We can provide two data elements to compare and an initializer list of several elements.
  • This function has several overloads for each inbuilt data type, and the comparison is made based on the inbuilt consideration of minimum and maximum. We can provide the compare function for two cases,
    • If we want to override the traditional/inbuilt comparison, for example, if we want to consider 2 as minimum and 1 as maximum, then C++ will not stop us. We can always define our compare function.
    • If we have provided a custom type to the min function, then comparisons cannot be made implicitly, so it is necessary to pass a compare function.
  • This function is declared in the algorithm header of the Standard Template Library.
  • Scope resolution is important while calling this function,
    • Either we can use a standard namespace, like this using namespace std;,
    • Or we can use the scope resolution operator along with the function name like this, std::min.
  • The value of variables provided to the function will never change because it accepts constant reference inside the function definition.