min function is used to compare the minimum of two or more values. It takes arguments a and b and returns the smallest of them. If you use comparator comp, you can customize the comparison rules. The min function can also be used to compare multiple values by passing them as function arguments.
Usage of min function in c
Introduction to min function
## The #min function is a standard library function used to compare the minimum of two or more values. It accepts two or more parameters and returns the smallest value among them.Function Syntax
<code class="cpp">#include <algorithm> template<class T> const T& min(const T& a, const T& b); template<class T, class Compare> const T& min(const T& a, const T& b, Compare comp);</code>
Parameters
and
b : Two or more values to compare.
: Optional comparator for custom comparison rules (only the second function prototype exists).
Return type
#min The function return type is the same as the parameter type, representing the minimum value.
Usage example
Compare the minimum value of two numbers:<code class="cpp">int a = 10; int b = 5; int min_value = min(a, b); // min_value 为 5</code>
<code class="cpp">struct MyComparator { bool operator()(const int& a, const int& b) { return a > b; } }; int a = 10; int b = 5; int max_value = min(a, b, MyComparator()); // max_value 为 10</code>
Note:
function will return the first parameter.
Functions can also be used to compare multiple values by passing them as function arguments.
The above is the detailed content of How to use min function in c++. For more information, please follow other related articles on the PHP Chinese website!