Namespaces
Variants

std:: minmax

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
minmax
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
헤더 파일에 정의됨 <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,

Compare comp ) ;
(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,

Compare comp ) ;
(4) (C++11부터)
(C++14부터 constexpr)

주어진 값들 중 가장 작은 값과 가장 큰 값을 반환합니다.

1,2) a b 중 더 작은 값과 더 큰 값에 대한 참조를 반환합니다.
1) 값 비교에 operator < 를 사용합니다.
만약 T LessThanComparable 가 아니라면, 동작은 정의되지 않습니다.
2) 값을 비교하기 위해 비교 함수 comp 를 사용하십시오.
3,4) 초기화 리스트에 있는 값들 중 가장 작은 값과 가장 큰 값을 반환합니다 ilist .
만약 ilist. size ( ) 가 0이거나, T CopyConstructible 가 아닌 경우, 동작은 정의되지 않습니다.
3) 값 비교에 operator < 를 사용합니다.
만약 T LessThanComparable 가 아니라면, 동작은 정의되지 않습니다.
4) 값을 비교하기 위해 비교 함수 comp 를 사용하십시오.

목차

매개변수

a, b - 비교할 값들
ilist - 비교할 값들을 가진 초기화 리스트
comp - 비교 함수 객체(즉, Compare 요구사항을 만족하는 객체)로 첫 번째 인수가 두 번째 인수보다 작은 경우 true 를 반환합니다.

비교 함수의 시그니처는 다음에 해당해야 합니다:

bool cmp ( const Type1 & a, const Type2 & b ) ;

시그니처에 const & 가 필요하지는 않지만, 함수는 전달된 객체를 수정해서는 안 되며 값 범주 에 관계없이 (가능한 const) Type1 Type2 타입의 모든 값을 수용할 수 있어야 합니다 (따라서 Type1 & 는 허용되지 않으며, Type1 에 대해 이동이 복사와 동등하지 않는 한 Type1 도 허용되지 않습니다 (C++11부터) ).
Type1 Type2 타입은 T 타입의 객체가 두 타입 모두로 암시적으로 변환될 수 있어야 합니다.

반환값

1,2) std:: pair < const T & , const T & > ( a, b ) 의 결과를 반환합니다. 단, a < b 이거나 a b 와 동등한 경우입니다. std:: pair < const T & , const T & > ( b, a ) 의 결과를 반환합니다. 단, b < a 인 경우입니다.
3,4) 첫 번째 요소로는 ilist 내의 가장 작은 값을 가진 요소를, 두 번째 요소로는 가장 큰 값을 가진 요소를 갖는 쌍. 가장 작은 값에 해당하는 요소가 여러 개인 경우, 가장 왼쪽에 있는 요소가 반환됩니다. 가장 큰 값에 해당하는 요소가 여러 개인 경우, 가장 오른쪽에 있는 요소가 반환됩니다.

복잡도

1) 정확히 한 번의 비교를 operator < 를 사용하여 수행합니다.
2) 비교 함수를 정확히 한 번 적용 comp .
3,4) 주어진 N ilist. size ( ) 로:
3) 최대
3N
2
번의 비교를 operator < 를 사용하여 수행합니다.
4) 최대
3N
2
번의 비교 함수 comp 적용.

가능한 구현

minmax (1)
template<class T>
constexpr std::pair<const T&, const T&> minmax(const T& a, const T& b)
{
    return (b < a) ? std::pair<const T&, const T&>(b, a)
                   : std::pair<const T&, const T&>(a, b);
}
minmax (2)
template<class T, class Compare>
constexpr std::pair<const T&, const T&> minmax(const T& a, const T& b, Compare comp)
{
    return comp(b, a) ? std::pair<const T&, const T&>(b, a)
                      : std::pair<const T&, const T&>(a, b);
}
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)
범위 내에서 가장 작은 요소와 가장 큰 요소를 반환합니다
(function template)
두 요소 중 더 작은 값과 더 큰 값을 반환합니다
(algorithm function object)