std::ranges::nth_element
cppreference.net에서
| 헤더에 정의됨 <algorithm>
|
||
| 호출 시그니처 |
||
template< std::random_access_iterator I, std::sentinel_for<I> S,
class Comp = ranges::less, class Proj = std::identity >
requires std::sortable<I, Comp, Proj>
constexpr I
nth_element( I first, I nth, S last, Comp comp = {}, Proj proj = {} );
|
(1) | (C++20부터) |
template< ranges::random_access_range R,
class Comp = ranges::less, class Proj = std::identity >
requires std::sortable<iterator_t<R>, Comp, Proj>
constexpr ranges::borrowed_iterator_t<R>
nth_element( R&& r, iterator_t<R> nth, Comp comp = {}, Proj proj = {} );
|
(2) | (C++20부터) |
다음과 같이 [first, last)의 요소를 재정렬합니다:
- 가 가리키는 요소는
nth는[first,last)에 대해comp및proj으로 정렬되었을 때 그 위치에 올 요소로 변경됩니다. - 이 새로운
nth요소 앞의 모든 요소는 이하(less than or equal to) 새로운nth요소 뒤의 요소들보다 작거나 같습니다. 즉, 모든 반복자 i, j에 대해, 범위[first,nth)및[nth,last)에서 각각 식std::invoke(comp, std::invoke(proj, *j), std::invoke(proj, *i))은false로 평가됩니다. - 만약
nth == last이면 함수는 아무 효과가 없습니다.
1) 요소는 주어진 이진 비교 함수 객체
comp와 투영 객체 proj를 사용하여 비교됩니다.2) (1)과 동일하지만,
r을 범위로 사용하며, ranges::begin(r)을 first로, ranges::end(r)을 last로 사용하는 것과 같습니다.이 페이지에서 설명하는 함수형 엔티티는 알고리즘 함수 객체 (비공식적으로 niebloids라고도 함)이며, 즉:
- 이들 중 하나를 호출할 때 명시적 템플릿 인자 목록을 지정할 수 없습니다.
- 이들 중 어느 것도 인자 종속 조회에 보이지 않습니다.
- 이들 중 하나가 일반적인 비한정 조회에 의해 함수 호출 연산자의 왼쪽에 있는 이름으로 발견되면, 인자 종속 조회가 억제됩니다.
매개변수
| first, last | - | 재정렬할 요소들의 범위 를 정의하는 반복자-감시자 쌍 |
| r | - | 재정렬할 요소들의 범위 |
| nth | - | 분할 지점을 정의하는 반복자 |
| comp | - | 투영된 요소들을 비교하는 데 사용되는 비교자 |
| proj | - | 요소들에 적용할 투영 |
반환값
1)
last
와 동일한 반복자.
복잡도
평균적으로 ranges:: distance ( first, last ) 에 선형입니다.
참고 사항
사용된 알고리즘은 일반적으로 introselect 이지만, 적절한 평균 경우 복잡도를 가진 다른 selection algorithms 도 허용됩니다.
가능한 구현
구현은 다음에서도 확인할 수 있습니다: msvc stl , libstdc++ , 그리고 libc++: (1) / (2) .
예제
이 코드 실행
#include <algorithm> #include <array> #include <functional> #include <iostream> #include <ranges> #include <string_view> void print(std::string_view rem, std::ranges::input_range auto const& a) { for (std::cout << rem; const auto e : a) std::cout << e << ' '; std::cout << '\n'; } int main() { std::array v{5, 6, 4, 3, 2, 6, 7, 9, 3}; print("Before nth_element: ", v); std::ranges::nth_element(v, v.begin() + v.size() / 2); print("After nth_element: ", v); std::cout << "The median is: " << v[v.size() / 2] << '\n'; std::ranges::nth_element(v, v.begin() + 1, std::greater<int>()); print("After nth_element: ", v); std::cout << "The second largest element is: " << v[1] << '\n'; std::cout << "The largest element is: " << v[0] << "\n\n"; using namespace std::literals; std::array names { "Diva"sv, "Cornelius"sv, "Munro"sv, "Rhod"sv, "Zorg"sv, "Korben"sv, "Bender"sv, "Leeloo"sv, }; print("Before nth_element: ", names); auto fifth_element{std::ranges::next(names.begin(), 4)}; std::ranges::nth_element(names, fifth_element); print("After nth_element: ", names); std::cout << "The 5th element is: " << *fifth_element << '\n'; }
출력:
Before nth_element: 5 6 4 3 2 6 7 9 3 After nth_element: 2 3 3 4 5 6 6 7 9 The median is: 5 After nth_element: 9 7 6 6 5 4 3 3 2 The second largest element is: 7 The largest element is: 9 Before nth_element: Diva Cornelius Munro Rhod Zorg Korben Bender Leeloo After nth_element: Diva Cornelius Bender Korben Leeloo Rhod Munro Zorg The 5th element is: Leeloo
같이 보기
(C++20) |
범위에서 가장 큰 요소를 반환합니다 (알고리즘 함수 객체) |
(C++20) |
범위에서 가장 작은 요소를 반환합니다 (알고리즘 함수 객체) |
(C++20) |
요소 범위를 두 그룹으로 나눕니다 (알고리즘 함수 객체) |
(C++20) |
범위의 처음 N개 요소를 정렬합니다 (알고리즘 함수 객체) |
| 범위가 정렬되었다면 N번째 요소를 찾습니다 (함수 템플릿) |