std:: minmax_element
|
헤더 파일에 정의됨
<algorithm>
|
||
|
template
<
class
ForwardIt
>
std::
pair
<
ForwardIt, ForwardIt
>
|
(1) |
(C++11부터)
(constexpr since C++17) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt
>
std::
pair
<
ForwardIt, ForwardIt
>
|
(2) | (C++17부터) |
|
template
<
class
ForwardIt,
class
Compare
>
std::
pair
<
ForwardIt, ForwardIt
>
|
(3) |
(C++11부터)
(constexpr since C++17) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
Compare
>
std::
pair
<
ForwardIt, ForwardIt
>
|
(4) | (C++17부터) |
범위
[
first
,
last
)
내에서 가장 작은 요소와 가장 큰 요소를 찾습니다.
|
std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> 가 true 인 경우. |
(C++20 이전) |
|
std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> 가 true 인 경우. |
(C++20 이후) |
목차 |
매개변수
| first, last | - | 검사할 요소들의 범위 를 정의하는 반복자 쌍 |
| policy | - | 사용할 실행 정책 |
| cmp | - |
비교 함수 객체(즉,
Compare
요구사항을 만족하는 객체)로 첫 번째 인수가 두 번째 인수보다
작은
경우
true
를 반환함.
비교 함수의 시그니처는 다음과 동일해야 함: bool cmp ( const Type1 & a, const Type2 & b ) ;
시그니처가
const
&
를 가질 필요는 없지만, 함수는 전달된 객체를 수정하지 않아야 하며
값 범주
에 관계없이 (가능한 const)
|
| 타입 요구사항 | ||
-
ForwardIt
는
LegacyForwardIterator
의 요구사항을 충족해야 함.
|
||
반환값
첫 번째 요소로 가장 작은 요소에 대한 반복자, 두 번째 요소로 가장 큰 요소에 대한 반복자로 구성된 쌍을 반환합니다. 범위가 비어 있는 경우 std:: make_pair ( first, first ) 를 반환합니다. 여러 요소가 가장 작은 요소와 동등한 경우, 첫 번째 해당 요소에 대한 반복자가 반환됩니다. 여러 요소가 가장 큰 요소와 동등한 경우, 마지막 해당 요소에 대한 반복자가 반환됩니다.
복잡도
주어진 N 이 std:: distance ( first, last ) 인 경우:
| 3 |
| 2 |
예외
ExecutionPolicy
라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:
-
알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고
ExecutionPolicy가 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른ExecutionPolicy의 경우 동작은 구현에 따라 정의됩니다. - 알고리즘이 메모리 할당에 실패할 경우, std::bad_alloc 이 throw됩니다.
가능한 구현
| minmax_element |
|---|
template<class ForwardIt> std::pair<ForwardIt, ForwardIt> minmax_element(ForwardIt first, ForwardIt last) { using value_type = typename std::iterator_traits<ForwardIt>::value_type; return std::minmax_element(first, last, std::less<value_type>()); } |
| minmax_element |
template<class ForwardIt, class Compare> std::pair<ForwardIt, ForwardIt> minmax_element(ForwardIt first, ForwardIt last, Compare comp) { auto min = first, max = first; if (first == last || ++first == last) return {min, max}; if (comp(*first, *min)) min = first; else max = first; while (++first != last) { auto i = first; if (++first == last) { if (comp(*i, *min)) min = i; else if (!(comp(*i, *max))) max = i; break; } else { if (comp(*first, *i)) { if (comp(*first, *min)) min = first; if (!(comp(*i, *max))) max = i; } else { if (comp(*i, *min)) min = i; if (!(comp(*first, *max))) max = first; } } } return {min, max}; } |
참고 사항
이 알고리즘은 std:: make_pair ( std:: min_element ( ) , std:: max_element ( ) ) 와 효율성 측면에서만 다른 것이 아니라, 이 알고리즘은 마지막 최대 요소를 찾는 반면 std::max_element 는 첫 번째 최대 요소를 찾는다는 점에서도 다릅니다.
예제
#include <algorithm> #include <iostream> int main() { const auto v = {3, 9, 1, 4, 2, 5, 9}; const auto [min, max] = std::minmax_element(begin(v), end(v)); std::cout << "min = " << *min << ", max = " << *max << '\n'; }
출력:
min = 1, max = 9
참고 항목
|
범위 내 가장 작은 요소를 반환
(function template) |
|
|
범위 내 가장 큰 요소를 반환
(function template) |
|
|
(C++20)
|
범위 내 가장 작은 요소와 가장 큰 요소를 반환
(algorithm function object) |