function template
std::min
<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 );
Return the lesser of two arguments
Returns the lesser of
a and
b. If both are equivalent,
a is returned.
The comparison uses
operator< for the first version, and
comp for the second.
The behavior of this function template is equivalent to:
1 2 3
|
template <class T> const T& min ( const T& a, const T& b ) {
return !(b<a)?a:b; // or: return !comp(b,a)?a:b; for the comp version
}
|
Parameters
- a, b
- Items to compare.
T is any type supporting copy constructions and comparisons with operator<.
- comp
- Comparison function object that, taking two values of the same type, returns true if the first argument is to be considered less than the second, and false otherwise.
Return value
The lesser of its two arguments.
Example
1 2 3 4 5 6 7 8 9 10 11 12
|
// min example
#include <iostream>
#include <algorithm>
using namespace std;
int main () {
cout << "min(1,2)==" << min(1,2) << endl;
cout << "min(2,1)==" << min(2,1) << endl;
cout << "min('a','z')==" << min('a','z') << endl;
cout << "min(3.14,2.72)==" << min(3.14,2.72) << endl;
return 0;
}
|
Output:
min(1,2)==1
min(2,1)==1
min('a','z')==a
min(3.14,2.72)==2.72
|
See also
- max
- Return the greater of two arguments (function template
)
- min_element
- Return smallest element in range (function template)