std::max
From Cppreference
|  Defined in header  <algorithm> | ||
| template< class T >  const T& max( const T& a, const T& b ); | (1) | |
| template< class T, class Compare > const T& max( const T& a, const T& b, Compare comp ); | (2) | |
Returns the greater 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 greater 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) ? b : a; } | 
| Second version: | 
| template<class T, class Compare> const T& max(const T& a, const T& b, Compare comp) { return (comp(a, b)) ? b : a; } | 
Example
#include <algorithm> #include <iostream> int main() { std::cout << "larger of 1 and 9999: " << std::max(1, 9999) << std::endl; std::cout << "larger of 'a' and 'b': " << std::max('a', 'b') << std::endl; std::cout << "larger of 3.14159 and 2.71828: " << std::max(3.14159, 2.71828) << std::endl; return 0; }
Output:
larger of 1 and 9999: 9999 larger of 'a' and 'b': b larger of 3.14159 and 2.71828: 3.14159
See also
| 
 | returns the largest element in a range (function template) | |
| 
 | returns the smaller of two elements (function template) | |
