📜  C++ min - C++ (1)

📅  最后修改于: 2023-12-03 15:13:54.697000             🧑  作者: Mango

C++ min

Introduction

C++ min is a standard library function in C++ that is used to find the minimum of two values. The function returns the smaller of the two arguments passed to it.

Syntax

The syntax for C++ min function is as follows:

template <class T> 
const T& min ( const T& a, const T& b );
Parameters

The C++ min function takes two arguments:

  • a: The first value to compare.
  • b: The second value to compare.

Both arguments have to be of the same type, or atleast the first argument has to be implicitly convertible to the type of the second argument.

Return Value

C++ min function returns the smaller of the two arguments passed to it.

Example

Here's an example of how to use the C++ min function:

#include <iostream>
#include <algorithm>

int main() {
    int a = 10, b = 20;
    int result = std::min(a, b);
    std::cout << "Minimum value between " << a << " and " << b << " is " << result << std::endl;
    return 0;
}

Output:

Minimum value between 10 and 20 is 10
Conclusion

C++ min function is a handy utility function for finding the minimum of two values. It's easy to use and can make your code more readable and efficient.