C++ std::min
The std::min function in C++ returns the smaller of two values or, in the case of an initializer list, the smallest element in the list. This function is used in comparisons to determine the minimum value.
Syntax of std::min
</>
Copy
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);
template <class T>
T min(std::initializer_list<T> ilist);
template <class T, class Compare>
T min(std::initializer_list<T> ilist, Compare comp);
Parameters of std::min
| Parameter | Description |
|---|---|
a, b | The two values to compare. |
ilist | An initializer list containing values to compare. |
comp (optional) | A binary comparison function that defines the comparison criteria. Defaults to <. |
Return Value of std::min
Returns the smaller of the two arguments, or the smallest element in the initializer list, based on the comparison criteria.
Examples for std::min
Example 1: Basic Usage of std::min
This example demonstrates finding the minimum of two integers:
</>
Copy
#include <iostream>
#include <algorithm>
int main() {
int a = 10, b = 20;
std::cout << "Minimum of " << a << " and " << b << " is: " << std::min(a, b) << std::endl;
return 0;
}
Output:
Minimum of 10 and 20 is: 10
Explanation:
- We declared two integer variables,
aandb, and initialized them with the values10and20respectively. - The
std::minfunction is used to find the smaller of the two values. It comparesaandband returns the smaller value. - The result of the comparison is printed to the console using
std::cout, along with an explanatory message. - The program terminates successfully after displaying the result.
Example 2: Using a Custom Comparison Function for std::min
You can use a custom comparison function to define the criteria for finding the minimum:
</>
Copy
#include <iostream>
#include <algorithm>
bool abs_compare(int a, int b) {
return std::abs(a) < std::abs(b);
}
int main() {
int a = -10, b = 5;
std::cout << "Minimum by absolute value of " << a << " and " << b << " is: " << std::min(a, b, abs_compare) << std::endl;
return 0;
}
Output:
Minimum by absolute value of -10 and 5 is: 5
Explanation:
- We defined a custom comparison function named
abs_compare. This function takes two integers as arguments and compares their absolute values usingstd::abs. - We declared two integer variables,
aandb, initialized with the values-10and5respectively. - The
std::minfunction is used with three arguments:a,b, and the custom comparison functionabs_compare. It returns the value with the smaller absolute magnitude based on the custom comparison logic.
Exception Handling in std::min
The std::min function does not throw exceptions on its own, but the comparison function passed as an argument may throw exceptions.
Example 1: Exception in Custom Comparison Function
In this example, the custom comparison function throws an exception for specific values:
</>
Copy
#include <iostream>
#include <algorithm>
#include <stdexcept>
bool faulty_compare(int a, int b) {
if (a == 5 || b == 5) {
throw std::runtime_error("Comparison involving 5 is not allowed.");
}
return a < b;
}
int main() {
try {
int a = 5, b = 10;
std::cout << "Minimum is: " << std::min(a, b, faulty_compare) << std::endl;
} catch (const std::exception& e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
}
return 0;
}
Output:
Exception caught: Comparison involving 5 is not allowed.
Explanation:
- We defined a custom comparison function named
faulty_compare. This function throws astd::runtime_errorif either of the integers being compared is 5. Otherwise, it compares the integers in ascending order. - We declared two integer variables,
aandb, initialized with the values5and10, respectively. - A
tryblock is used to safely execute thestd::minfunction witha,b, and the custom comparison functionfaulty_compare. - When
faulty_compareencounters the value5, it throws astd::runtime_errorexception with a descriptive message. - The
catchblock captures the exception and prints the error message to the console usingstd::cerr.
