std::max
| 헤더 <algorithm>
|
||
template< class T >
const T& max( const T& a, const T& b );
|
에 정의됨 (1) | (C++14부터 constexpr) |
template< class T, class Compare >
const T& max( const T& a, const T& b, Compare comp );
|
(2) | (C++14부터 constexpr) |
template< class T >
T max( std::initializer_list<T> ilist );
|
(3) | (C++11부터) (C++14부터 constexpr) |
template< class T, class Compare >
T max( std::initializer_list<T> ilist, Compare comp );
|
(4) | (C++11부터) (C++14부터 constexpr) |
주어진 값들 중 더 큰 값을 반환합니다.
a와 b 중 더 큰 값을 반환합니다.operator<을 사용합니다.T이 LessThanComparable이 아닌 경우 동작은 정의되지 않습니다.comp을 사용합니다.ilist의 값들 중 가장 큰 값을 반환합니다.operator<을 사용합니다.T이 LessThanComparable이 아닌 경우 동작은 정의되지 않습니다.comp을 사용합니다.매개변수
| a, b | - | 비교할 값들 |
| ilist | - | 비교할 값들을 가진 초기화자 리스트 |
| comp | - |
비교 함수 객체(즉,
Compare
요구 사항을 만족하는 객체)로
a
가
b
보다
작은
경우
true
를 반환합니다.
비교 함수의 시그니처는 다음에 해당해야 합니다: bool cmp ( const Type1 & a, const Type2 & b ) ;
시그니처에
const
&
가 필요하지는 않지만, 함수는 전달된 객체를 수정해서는 안 되며
값 범주
에 관계없이 (가능한 const)
|
반환값
복잡도
가능한 구현
| 최대값 (1) |
|---|
template<class T> const T& max(const T& a, const T& b) { return (a < b) ? b : a; } |
| 최대값 (2) |
template<class T, class Compare> const T& max(const T& a, const T& b, Compare comp) { return (comp(a, b)) ? b : a; } |
| 최대값 (3) |
template<class T> T max(std::initializer_list<T> ilist) { return *std::max_element(ilist.begin(), ilist.end()); } |
| 최대값 (4) |
template<class T, class Compare> T max(std::initializer_list<T> ilist, Compare comp) { return *std::max_element(ilist.begin(), ilist.end(), comp); } |
참고 사항
std::max
의 결과를 참조로 캡처할 때, 매개변수 중 하나가 임시 객체이고 해당 매개변수가 반환되면 댕글링 참조가 발생합니다:
int n = -1; const int& r = std::max(n + 2, n * 2); // r은 댕글링 참조입니다
예제
#include <algorithm> #include <iomanip> #include <iostream> #include <string_view> int main() { auto longest = [](const std::string_view s1, const std::string_view s2) { return s1.size() < s2.size(); }; std::cout << "Larger of 69 and 96 is " << std::max(69, 96) << "\n" "Larger of 'q' and 'p' is '" << std::max('q', 'p') << "'\n" "Largest of 010, 10, 0X10, and 0B10 is " << std::max({010, 10, 0X10, 0B10}) << '\n' << R"(Longest of "long", "short", and "int" is )" << std::quoted(std::max({"long", "short", "int"}, longest)) << '\n'; }
출력:
Larger of 69 and 96 is 96 Larger of 'q' and 'p' is 'q' Largest of 010, 10, 0X10, and 0B10 is 16 Longest of "long", "short", and "int" is "short"
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 281 | C++98 |
T
가
CopyConstructible
요구사항을 만족해야 했음
(
1,2
)
오버로드에 대해
|
요구사항이 아님 |
| LWG 2239 |
C++98
C++11 |
1.
T
가
LessThanComparable
요구사항을 만족해야 했음
( 2 ) (C++98) 및 ( 4 ) (C++11) 오버로드에 대해 2. 복잡도 요구사항이 누락됨 |
1. 요구사항이 아님
2. 요구사항 추가됨 |
참고 항목
| 주어진 값 중 더 작은 값을 반환합니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
|
(C++11) |
두 요소 중 더 작은 값과 더 큰 값을 반환합니다 (함수 템플릿 & 알고리즘 함수 객체) |
(C++20) |
|
| 범위 내에서 가장 큰 요소를 반환합니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
|
(C++17) |
값을 경계값 쌍 사이에 고정합니다 (함수 템플릿 & 알고리즘 함수 객체) |
(C++20) |
|
(C++20) |
주어진 값 중 더 큰 값을 반환합니다 (알고리즘 함수 객체) |