std::min
From Cppreference
Defined in header
<algorithm> | ||
template< class T >
const T& min( const T& a, const T& b ); | (1) | |
template< class T, class Compare >
const T& min( const T& a, const T& b, Compare comp ); | (2) | |
Returns the lesser of two values. The first version uses operator< to compare the values, the second version uses the given comparison function comp.
Contents |
Parameters
a, b | - | the values to compare | |||||||||
cmp | - | comparison function which returns true if if a is less than b. The signature of the comparison function should be equivalent to the following:
The signature does not need to have const &, but the function must not modify the objects passed to it. |
Return value
The smaller of a and b.
Complexity
constant
Equivalent function
First version: |
---|
template<class T> const T& max(const T& a, const T& b) { return (a < b) ? a : b; } |
Second version: |
template<class T, class Compare> const T& max(const T& a, const T& b, Compare comp) { return (comp(a, b)) ? a : b; } |
Example
#include <algorithm> #include <iostream> int main() { std::cout << "smaller of 1 and 9999: " << std::min(1, 9999) << std::endl; std::cout << "smaller of 'a' and 'b': " << std::min('a', 'b') << std::endl; std::cout << "smaller of 3.14159 and 2.71828: " << std::min(3.14159, 2.71828) << std::endl; return 0; }
Output:
smaller of 1 and 9999: 9999 smaller of 'a' and 'b': b smaller of 3.14159 and 2.71828: 3.14159
See also
| returns the smallest element in a range (function template) | |
| returns the larger of two elements (function template) |