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