On this page
std::ranges::partial_sort_copy, std::ranges::partial_sort_copy_result
Defined in header <algorithm> |
||
|---|---|---|
| Call signature | ||
|
(1) | (since C++20) |
|
(2) | (since C++20) |
| Helper types | ||
|
(3) | (since C++20) |
Copies the first N elements from the source range [first, last), as if it was partially sorted with respect to comp and proj1, into the destination range [result_first, result_first + N), where \(\scriptsize N = \min{(L_1, L_2)}\)N = min(L₁, L₂), \(\scriptsize L_1\)L₁ is equal to ranges::distance(first, last), and \(\scriptsize L_2\)L₂ is equal to ranges::distance(result_first, result_last).
The order of equal elements is not guaranteed to be preserved.
proj1, and the destination elements are projected using the function object proj2.
r as the source range and result_r as the destination range, as if using ranges::begin(r) as first, ranges::end(r) as last, ranges::begin(result_r) as result_first, and ranges::end(result_r) as result_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 source range to copy from |
| r | - | the source range to copy from |
| result_first, result_last | - | iterator-sentinel defining the destination range |
| result_r | - | the destination range |
| comp | - | comparison to apply to the projected elements |
| proj1 | - | projection to apply to the elements of source range |
| proj2 | - | projection to apply to the elements of destination range |
Return value
An object equal to {last, result_first + N}.
Complexity
At most \(\scriptsize L_1 \cdot \log{(N)}\)L₁•log(N) comparisons and \(\scriptsize 2 \cdot L_1 \cdot \log{(N)}\)2•L₁•log(N) projections.
Possible implementation
|
Example
#include <algorithm>
#include <forward_list>
#include <functional>
#include <iostream>
#include <ranges>
#include <string_view>
#include <vector>
void print(std::string_view rem, std::ranges::input_range auto const& v)
{
for (std::cout << rem; const auto& e : v)
std::cout << e << ' ';
std::cout << '\n';
}
int main()
{
const std::forward_list source{4, 2, 5, 1, 3};
print("Write to the smaller vector in ascending order: ", "");
std::vector dest1{10, 11, 12};
print("const source list: ", source);
print("destination range: ", dest1);
std::ranges::partial_sort_copy(source, dest1);
print("partial_sort_copy: ", dest1);
print("Write to the larger vector in descending order:", "");
std::vector dest2{10, 11, 12, 13, 14, 15, 16};
print("const source list: ", source);
print("destination range: ", dest2);
std::ranges::partial_sort_copy(source, dest2, std::greater{});
print("partial_sort_copy: ", dest2);
}
Output:
Write to the smaller vector in ascending order:
const source list: 4 2 5 1 3
destination range: 10 11 12
partial_sort_copy: 1 2 3
Write to the larger vector in descending order:
const source list: 4 2 5 1 3
destination range: 10 11 12 13 14 15 16
partial_sort_copy: 5 4 3 2 1 15 16
See also
|
(C++20)
|
sorts the first N elements of a range (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)
|
turns a max heap into a range of elements sorted in ascending order (niebloid) |
|
(C++20)
|
creates a max heap out of a range of elements (niebloid) |
|
(C++20)
|
adds an element to a max heap (niebloid) |
|
(C++20)
|
removes the largest element from a max heap (niebloid) |
| copies and partially sorts a range of elements (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_copy