Files
cppdraft_translate/cppdraft/alg/nth/element.md
2025-10-25 03:02:53 +03:00

6.2 KiB
Raw Blame History

[alg.nth.element]

26 Algorithms library [algorithms]

26.8.3 Nth element [alg.nth.element]

🔗

`template constexpr void nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last); template<class ExecutionPolicy, class RandomAccessIterator> void nth_element(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);

template<class RandomAccessIterator, class Compare> constexpr void nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp); template<class ExecutionPolicy, class RandomAccessIterator, class Compare> void nth_element(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);

template<random_access_iterator I, sentinel_for S, class Comp = ranges::less, class Proj = identity> requires sortable<I, Comp, Proj> constexpr I ranges::nth_element(I first, I nth, S last, Comp comp = {}, Proj proj = {}); template<execution-policy Ep, random_access_iterator I, sized_sentinel_for S, class Comp = ranges::less, class Proj = identity> requires sortable<I, Comp, Proj> I ranges::nth_element(Ep&& exec, I first, I nth, S last, Comp comp = {}, Proj proj = {}); `

1

#

Let comp be less{} and proj be identity{} for the overloads with no parameters by those names.

2

#

Preconditions: [first, nth) and [nth, last) are valid ranges.

For the overloads in namespace std,RandomAccessIterator meets the Cpp17ValueSwappable requirements ([swappable.requirements]), and the type of *first meets the Cpp17MoveConstructible (Table 31) andCpp17MoveAssignable (Table 33) requirements.

3

#

Effects: After nth_element the element in the position pointed to by nth is the element that would be in that position if the whole range were sorted with respect to comp and proj, unless nth == last.

Also for every iterator i in the range [first, nth) and every iterator j in the range [nth, last) it holds that:bool(invoke(comp, invoke(proj, *j), invoke(proj, *i))) is false.

4

#

Returns: last for the overload in namespace ranges.

5

#

Complexity: For the non-parallel algorithm overloads, linear on average.

For the parallel algorithm overloads, O(N) applications of the predicate, and O(NlogN) swaps, where N=last - first.

🔗

template<[random_access_range](range.refinements#concept:random_access_range "25.4.6Other range refinements[range.refinements]") R, class Comp = ranges::less, class Proj = identity> requires [sortable](alg.req.sortable#concept:sortable "24.3.7.8Concept sortable[alg.req.sortable]")<iterator_t<R>, Comp, Proj> constexpr borrowed_iterator_t<R> ranges::nth_element(R&& r, iterator_t<R> nth, Comp comp = {}, Proj proj = {});

6

#

Effects: Equivalent to:return ranges::nth_element(ranges::begin(r), nth, ranges::end(r), comp, proj);

🔗

template<[execution-policy](algorithms.parallel.defns#concept:execution-policy "26.3.1Preamble[algorithms.parallel.defns]") Ep, [sized-random-access-range](range.refinements#concept:sized-random-access-range "25.4.6Other range refinements[range.refinements]") R, class Comp = ranges::less, class Proj = identity> requires [sortable](alg.req.sortable#concept:sortable "24.3.7.8Concept sortable[alg.req.sortable]")<iterator_t<R>, Comp, Proj> borrowed_iterator_t<R> ranges::nth_element(Ep&& exec, R&& r, iterator_t<R> nth, Comp comp = {}, Proj proj = {});

7

#

Effects: Equivalent to:return ranges::nth_element(std::forward(exec), ranges::begin(r), nth, ranges::end(r), comp, proj);