max

max (두 값 중 큰 값)

두 값(또는 초기화자 목록) 중 더 큰 값을 반환하는 알고리즘이에요. <algorithm> 헤더에 있어요.

출처: cppreference

본문

max는 두 값 중 큰 값을 반환해요.

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)

template< class T >
T max( std::initializer_list<T> ilist );   // (3)
    1. operator<로, 2) comp로 비교해요. 3) 목록 중 최대값을 값으로 반환해요.
  • 두 값이 같으면 a(첫 인자)를 반환해요.
int x = std::max(3, 7);        // 7
int y = std::max({3, 9, 5});   // 9

인자가 참조라서 큰 값의 참조를 돌려준다는 점을 알아두세요. min과 짝을 이루고, 임의 개수의 최대값이 필요하면 초기화자 목록 버전을 써요.

더 알아보기 (Learn more)

cppreference