std::partial_sort_copy
| Defined in header <algorithm>
|
||
template< class InputIt, class RandomIt >
RandomIt partial_sort_copy( InputIt first, InputIt last,
RandomIt d_first, RandomIt d_last );
|
(1) | (constexpr since C++20) |
template< class InputIt, class RandomIt, class Compare >
RandomIt partial_sort_copy( InputIt first, InputIt last,
RandomIt d_first, RandomIt d_last,
Compare comp );
|
(2) | (constexpr since C++20) |
template< class ExecutionPolicy,
class ForwardIt, class RandomIt >
RandomIt partial_sort_copy( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last,
RandomIt d_first, RandomIt d_last );
|
(3) | (since C++17) |
template< class ExecutionPolicy,
class ForwardIt, class RandomIt, class Compare >
RandomIt partial_sort_copy( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last,
RandomIt d_first, RandomIt d_last,
Compare comp );
|
(4) | (since C++17) |
Copies elements from the source range [first, last) as sorted into the destination range [d_first, d_last):
- If
std::distance(first, last)is greater thand_last - d_first, the firstd_last - d_firstelements of the source range is copied as sorted into[d_first,d_last). - Otherwise, all elements of the source range is copied as sorted into
[d_first,d_first + std::distance(first, last)).
comp.policy.true:
|
|
(until C++20) |
|
|
(since C++20) |
If *first is not writable to d_first, the program is ill-formed.
If any of the following conditions is satisfied, the behavior is undefined:
|
(until C++11) |
|
(since C++11) |
Parameters
| first, last | - | the pair of iterators defining the source range |
| d_first, d_last | - | the pair of iterators defining the destination range |
| comp | - | comparison function object (i.e. an object that satisfies the requirements of Compare) which returns true if the first argument is less than (i.e. is ordered before) the second. The signature of the comparison function should be equivalent to the following:
While the signature does not need to have |
| policy | - | the execution policy to use |
| Type requirements | ||
-InputIt must meet the requirements of LegacyInputIterator.
| ||
-ForwardIt must meet the requirements of LegacyForwardIterator.
| ||
-RandomIt must meet the requirements of LegacyRandomAccessIterator.
| ||
-Compare must meet the requirements of Compare.
| ||
Return value
An iterator past the last assigned element in the destination range, or d_first if no element is assigned.
Complexity
Given
- N1 as
std::distance(first, last), - N2 as
d_last - d_first:
operator<(until C++20)std::less{}(since C++20).comp.operator<(until C++20)std::less{}(since C++20).comp.Exceptions
- If the temporary memory resources required for parallelization are not available, std::bad_alloc is thrown.
- If an uncaught exception is thrown while accessing objects via an algorithm argument, the behavior is determined by the execution policy (for standard policies, std::terminate is invoked).
Possible implementation
See also the implementations in libstdc++ and libc++.
Example
#include <algorithm>
#include <cstdio>
#include <functional>
#include <print>
#include <string_view>
struct Result
{
std::string_view name;
double score;
friend std::partial_ordering operator<=>(Result lhs, Result rhs)
{
return lhs.score <=> rhs.score;
}
};
int main()
{
const Result results[]
{
{"Curt von Bardeleben", 11.5},
{"Emanuel Lasker", 15.5},
{"Emanuel Schiffers", 12},
{"Harry Pillsbury", 16.5},
{"Mikhail Chigorin", 16},
{"Richard Teichmann", 11.5},
{"Siegbert Tarrasch", 14},
{"William Steinitz", 13}
};
Result podium[3];
std::partial_sort_copy(
std::begin(results), std::end(results),
std::begin(podium), std::end(podium),
std::greater()
);
std::puts("Top 3:");
for (Result result : podium)
std::print(" {}: {}\n", result.name, result.score);
}
Output:
Top 3:
Harry Pillsbury: 16.5
Mikhail Chigorin: 16
Emanuel Lasker: 15.5
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| P0896R4 | C++98 | *first was not required to be writable to d_first
|
the program is ill-formed if not writable |
