function template
std::max
<algorithm>
template <class T> const T& max ( const T& a, const T& b );
template <class T, class Compare>
const T& max ( const T& a, const T& b, Compare comp );
Return the greater of two arguments
Returns the greater 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& max ( const T& a, const T& b ) {
return (a<b)?b:a; // or: return comp(a,b)?b:a; 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 greater of its two arguments
Example
1 2 3 4 5 6 7 8 9 10 11 12
|
// max example
#include <iostream>
#include <algorithm>
using namespace std;
int main () {
cout << "max(1,2)==" << max(1,2) << endl;
cout << "max(2,1)==" << max(2,1) << endl;
cout << "max('a','z')==" << max('a','z') << endl;
cout << "max(3.14,2.73)==" << max(3.14,2.73) << endl;
return 0;
}
|
Output:
max(1,2)==2
max(2,1)==2
max('a','z')==z
max(3.14,2.73)==3.14
|
See also
- min
- Return the lesser of two arguments (function template
)
- max_element
- Return largest element in range (function template
)