std::max_element
| 헤더에 정의됨 <algorithm>
|
||
template< class ForwardIt >
ForwardIt max_element( ForwardIt first, ForwardIt last );
|
(1) | (C++17부터 constexpr) |
template< class ExecutionPolicy, class ForwardIt >
ForwardIt max_element( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last );
|
(2) | (C++17부터) |
template< class ForwardIt, class Compare >
ForwardIt max_element( ForwardIt first, ForwardIt last,
Compare comp );
|
(3) | (C++17부터 constexpr) |
template< class ExecutionPolicy, class ForwardIt, class Compare >
ForwardIt max_element( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last,
Compare comp );
|
(4) | (C++17부터) |
범위에서 가장 큰 요소를 찾습니다 [first, last)
operator<(C++20까지)std::less{}(C++20부터).comp.policy에 따라 실행됩니다.true인 경우에만 오버로드 해결에 참여합니다:
|
|
(C++20까지) |
|
|
(C++20부터) |
매개변수
| first, last | - | 검사할 요소들을 정의하는 반복자 쌍 범위 의 요소들을 검사합니다 |
| policy | - | 실행 정책을 사용합니다 |
| comp | - | 비교 함수 객체 (즉, Compare의 요구 사항을 충족하는 객체) true를 반환합니다. 첫 번째 인수가 less보다 작은 경우.비교 함수의 시그니처는 다음과 같아야 합니다:
시그니처에 |
| 유형 요구 사항 | ||
-
ForwardIt는 LegacyForwardIterator의 요구 사항을 충족해야 합니다. | ||
반환값
범위에서 가장 큰 요소를 가리키는 반복자입니다. [first, last). 범위에 가장 큰 요소와 동등한 여러 요소가 있는 경우 첫 번째 그러한 요소를 가리키는 반복자를 반환합니다. 반환값: last (범위가 비어 있는 경우)
복잡도
주어진 N을(를) std::distance(first, last)로 할 때:
operator<(C++20까지)std::less{}(C++20부터) 사용)comp.예외
템플릿 매개변수 이름이 ExecutionPolicy인 오버로드는 다음과 같이 오류를 보고합니다:
- 알고리즘의 일부로 호출된 함수가 예외를 발생시키고
ExecutionPolicy가 표준 정책 중 하나인 경우 std::terminate가 호출됩니다. 다른ExecutionPolicy의 경우 동작은 구현에 따라 정의됩니다. - 알고리즘이 메모리 할당에 실패하면 std::bad_alloc이 발생합니다.
가능한 구현
| max_element (1) |
|---|
template<class ForwardIt>
ForwardIt max_element(ForwardIt first, ForwardIt last)
{
if (first == last)
return last;
ForwardIt largest = first;
while (++first != last)
if (*largest < *first)
largest = first;
return largest;
}
|
| max_element (3) |
template<class ForwardIt, class Compare>
ForwardIt max_element(ForwardIt first, ForwardIt last, Compare comp)
{
if (first == last)
return last;
ForwardIt largest = first;
while(++first != last)
if (comp(*largest, *first))
largest = first;
return largest;
}
|
예제
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v{3, 1, -14, 1, 5, 9, -14, 9};
std::vector<int>::iterator result;
result = std::max_element(v.begin(), v.end());
std::cout << "Max element found at index "
<< std::distance(v.begin(), result)
<< " has value " << *result << '\n';
result = std::max_element(v.begin(), v.end(), [](int a, int b)
{
return std::abs(a) < std::abs(b);
});
std::cout << "Absolute max element found at index "
<< std::distance(v.begin(), result)
<< " has value " << *result << '\n';
}
출력:
Max element found at index 5 has value 9
Absolute max element found at index 2 has value -14
결함 보고서
다음 동작 변경 결함 보고서는 이전에 발행된 C++ 표준에 소급하여 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 212 | C++98 | [first, last)이 비어 있는 경우 반환값이 지정되지 않았습니다.
|
이 경우 last를 반환합니다.
|
| LWG 2150 | C++98 | 가장 작지 않은 첫 번째 요소를 가리키는 반복자가 반환되었습니다. | 반환값을 수정했습니다. |
참고 항목
| 범위에서 가장 작은 요소를 반환합니다. (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
|
(C++11) |
범위에서 가장 작은 요소와 가장 큰 요소를 반환합니다. (함수 템플릿 & 알고리즘 함수 객체) |
(C++20) |
|
| 주어진 값 중 더 큰 값을 반환합니다. (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
|
(C++20) |
범위에서 가장 큰 요소를 반환합니다. (알고리즘 함수 객체) |