std:: minmax
|
헤더 파일에 정의됨
<algorithm>
|
||
|
template
<
class
T
>
std:: pair < const T & , const T & > minmax ( const T & a, const T & b ) ; |
(1) |
(C++11부터)
(C++14부터 constexpr) |
|
template
<
class
T,
class
Compare
>
std::
pair
<
const
T
&
,
const
T
&
>
minmax
(
const
T
&
a,
const
T
&
b,
|
(2) |
(C++11부터)
(C++14부터 constexpr) |
|
template
<
class
T
>
std:: pair < T, T > minmax ( std:: initializer_list < T > ilist ) ; |
(3) |
(C++11부터)
(C++14부터 constexpr) |
|
template
<
class
T,
class
Compare
>
std::
pair
<
T, T
>
minmax
(
std::
initializer_list
<
T
>
ilist,
|
(4) |
(C++11부터)
(C++14부터 constexpr) |
주어진 값들 중 가장 작은 값과 가장 큰 값을 반환합니다.
목차 |
매개변수
| a, b | - | 비교할 값들 |
| ilist | - | 비교할 값들을 가진 초기화 리스트 |
| comp | - |
비교 함수 객체(즉,
Compare
요구사항을 만족하는 객체)로 첫 번째 인수가 두 번째 인수보다
작은
경우
true
를 반환합니다.
비교 함수의 시그니처는 다음에 해당해야 합니다: bool cmp ( const Type1 & a, const Type2 & b ) ;
시그니처에
const
&
가 필요하지는 않지만, 함수는 전달된 객체를 수정해서는 안 되며
값 범주
에 관계없이 (가능한 const)
|
반환값
복잡도
| 3N |
| 2 |
| 3N |
| 2 |
가능한 구현
| minmax (1) |
|---|
| minmax (2) |
| minmax (3) |
template<class T> constexpr std::pair<T, T> minmax(std::initializer_list<T> ilist) { auto p = std::minmax_element(ilist.begin(), ilist.end()); return std::pair(*p.first, *p.second); } |
| minmax (4) |
template<class T, class Compare> constexpr std::pair<T, T> minmax(std::initializer_list<T> ilist, Compare comp) { auto p = std::minmax_element(ilist.begin(), ilist.end(), comp); return std::pair(*p.first, *p.second); } |
참고 사항
오버로드
(
1,2
)
의 경우, 매개변수 중 하나가 임시 객체일 때, 반환된 참조는
minmax
호출을 포함하는 전체 표현식이 끝날 때 댕글링 참조가 됩니다:
int n = 1; auto p = std::minmax(n, n + 1); int m = p.first; // 정상 int x = p.second; // 정의되지 않은 동작 // 구조적 바인딩도 동일한 문제가 있음 auto [mm, xx] = std::minmax(n, n + 1); xx; // 정의되지 않은 동작
예제
#include <algorithm> #include <cstdlib> #include <ctime> #include <iostream> #include <vector> int main() { std::vector<int> v{3, 1, 4, 1, 5, 9, 2, 6}; std::srand(std::time(0)); std::pair<int, int> bounds = std::minmax(std::rand() % v.size(), std::rand() % v.size()); std::cout << "v[" << bounds.first << "," << bounds.second << "]: "; for (int i = bounds.first; i < bounds.second; ++i) std::cout << v[i] << ' '; std::cout << '\n'; }
가능한 출력:
v[2,7]: 4 1 5 9 2
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 2239 | C++11 |
T
가
LessThanComparable
요구사항을 충족해야 했음
(
2,4
)
|
요구사항 아님 |
참고 항목
|
주어진 값 중 더 작은 값을 반환합니다
(function template) |
|
|
주어진 값 중 더 큰 값을 반환합니다
(function template) |
|
|
(C++11)
|
범위 내에서 가장 작은 요소와 가장 큰 요소를 반환합니다
(function template) |
|
(C++20)
|
두 요소 중 더 작은 값과 더 큰 값을 반환합니다
(algorithm function object) |