max


Return the maximum of two items.

Return the larger of a and b , or return a if a and b are equal. The first version uses operator< to perform the comparison, whereas the second version uses the binary function compare.

Library

Standards<ToolKit>

Declaration


#include <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 compare );

Complexity

Time and space complexity are constant.

Example <ospace/osstd/examples/max1.cpp>
#include <iostream>
#include <algorithm>

void
main()
  {
  cout << max( 42, 100 ) << "\n";
  }

100
Example <ospace/osstd/examples/max2.cpp>
#include <iostream>
#include <string.h>
#include <algorithm>

bool
str_compare( const char* a_, const char* b_ )
  {
  return ::strcmp( a_, b_ ) < 0 ? 1 : 0;
  }

void
main()
  {
  cout << max( "shoe", "shine", str_compare ) << "\n";
  }

shoe

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.