On this page
std::ranges::partial_sort
Defined in header <algorithm> |
||
---|---|---|
Call signature | ||
|
(1) | (since C++20) |
|
(2) | (since C++20) |
1) Rearranges elements such that the range
[
first
,
middle
)
contains the sorted middle - first
smallest elements in the range [
first
,
last
)
.
The order of equal elements is not guaranteed to be preserved. The order of the remaining elements in the range
[
middle
,
last
)
is unspecified.
The elements are compared using the given binary comparison function
comp
and projected using proj
function object.
2) Same as (1), but uses
r
as the range, as if using ranges::begin(r)
as first
and ranges::end(r)
as last
.
The function-like entities described on this page are niebloids, that is:
- Explicit template argument lists cannot be specified when calling any of them.
- None of them are visible to argument-dependent lookup.
- When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.
In practice, they may be implemented as function objects, or with special compiler extensions.
Parameters
first, last | - | iterator-sentinel defining the range to sort |
r | - | the range to sort |
middle | - | the iterator defining the last element to be sorted |
comp | - | comparator to apply to the projected elements |
proj | - | projection to apply to the elements |
Return value
An iterator equal to last
.
Complexity
\(\scriptsize \mathcal{O}(N\cdot\log{(M)})\)𝓞(N·log(M)) comparisons and twice as many projections, where \(\scriptsize N\)N is ranges::distance(first, last)
, \(\scriptsize M\)M is ranges::distance(first, middle)
.
Possible implementation
|
Example
#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
void print(const auto& v)
{
for (const char e : v)
std::cout << e << ' ';
std::cout << '\n';
}
void underscore(int n)
{
while (n-- > 0)
std::cout << "^ ";
std::cout << '\n';
}
int main()
{
static_assert('A' < 'a');
std::vector<char> v {'x', 'P', 'y', 'C', 'z', 'w', 'P', 'o'};
print(v);
const int m {3};
std::ranges::partial_sort(v, v.begin() + m);
print(v), underscore(m);
static_assert('1' < 'a');
std::string s {"3a1b41c5"};
print(s);
std::ranges::partial_sort(s.begin(), s.begin() + m, s.end(), std::greater {});
print(s), underscore(m);
}
Output:
x P y C z w P o
C P P y z x w o
^ ^ ^
3 a 1 b 4 1 c 5
c b a 1 3 1 4 5
^ ^ ^
See also
(C++20)
|
copies and partially sorts a range of elements (niebloid) |
(C++20)
|
sorts a range into ascending order (niebloid) |
(C++20)
|
sorts a range of elements while preserving order between equal elements (niebloid) |
(C++20)
|
partially sorts the given range making sure that it is partitioned by the given element (niebloid) |
(C++20)
|
creates a max heap out of a range of elements (niebloid) |
(C++20)
|
removes the largest element from a max heap (niebloid) |
(C++20)
|
adds an element to a max heap (niebloid) |
(C++20)
|
turns a max heap into a range of elements sorted in ascending order (niebloid) |
sorts the first N elements of a range (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/algorithm/ranges/partial_sort