The min()
function in C++ accepts two values and returns the smaller one. This function is available in <algorithm.h>
.
The min()
function accepts the following parameters.
first_value
: This is the first value that we want to compare.
second_value
: This is the second value to compare with the first one.
You need to pass the two values of same type.
comparator
: This is an optional parameter that specifies the condition when the first value should come and when the second value should come. In other words, you can pass a function that returns a Boolean value that denotes which value to pick out of the two given values.
The min()
function returns the minimum value from the two given values.
Let’s look at the code now.
#include <iostream>#include <algorithm>using namespace std;int main() {int int_result = min(10, 20);cout << "Minimum is: " << int_result << endl;char char_result = min('a', 'b');cout << "Minimum is: " << char_result;return 0;}
min()
function and pass two integer values.min()
function, but here we pass two character values.