C++ std::partial_sort_copy() Function

Definition
In C++, std::partial_sort_copy() copies the smallest elements from a source range into a destination range and sorts them. The source is not changed.
The number of copied elements is the smaller of the source size and the destination size.
Syntax
Add #include <algorithm>, then call std::partial_sort_copy(). Start with these two lines.
std::partial_sort_copy(src.begin(), src.end(), dst.begin(), dst.end());
std::partial_sort_copy(src.begin(), src.end(), dst.begin(), dst.end(), comp);
| Call | What it does |
|---|
std::partial_sort_copy(src.begin(), src.end(), dst.begin(), dst.end()); | Fill dst with the smallest values, sorted. |
std::partial_sort_copy(src.begin(), src.end(), dst.begin(), dst.end(), comp); | Use your own comparison. |
Library declarations
template<class InputIt, class RandomIt>
RandomIt partial_sort_copy(InputIt first, InputIt last,
RandomIt d_first, RandomIt d_last);
Parameters
| Parameter | Description |
|---|
| first, last | Required. The source range. |
| d_first, d_last | Required. The destination range. Its size decides how many elements are copied. |
| comp | Optional. A comparison function. |
Return Value
| Return Value | Description |
|---|
| Iterator | Points just after the last element written in the destination. |
C++ Examples
Example 1 — Three smallest into a new vector
Keep the source and get a sorted shortlist.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> src = {9, 4, 7, 1, 8};
std::vector<int> dest(3);
std::partial_sort_copy(src.begin(), src.end(), dest.begin(), dest.end());
std::cout << "src: ";
for (int x : src) std::cout << x << ' ';
std::cout << '\n';
std::cout << "dest: ";
for (int x : dest) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
src: 9 4 7 1 8
dest: 1 4 7
How It Works
dest has room for three elements, so three are copied.- They are the smallest values, sorted: 1, 4 and 7.
src is printed unchanged.
Example 2 — Destination bigger than source
All source values are copied and sorted.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> src = {3, 1, 2};
std::vector<int> dest(5, 0);
auto it = std::partial_sort_copy(src.begin(), src.end(), dest.begin(), dest.end());
std::cout << "Written: " << (it - dest.begin()) << '\n';
for (int x : dest) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
- The source has only three elements, so only three are written.
- The returned iterator shows that three slots were used.
- The last two slots of
dest keep their original 0 values.
Example 3 — Two largest
Use std::greater to copy the biggest values.
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
int main() {
std::vector<int> src = {50, 20, 80, 10};
std::vector<int> top(2);
std::partial_sort_copy(src.begin(), src.end(), top.begin(), top.end(),
std::greater<int>());
for (int x : top) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
std::greater<int>() makes larger numbers count as "first".- The two largest values are 80 and 50, in that order.
- The destination size of 2 limits the result.
Frequently Asked Questions
In C++, std::partial_sort_copy() copies the smallest elements of a range into a sorted destination. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns an iterator to the end of the sorted elements written in the destination.
partial_sort() rearranges the source. partial_sort_copy() leaves the source alone and writes the sorted shortlist elsewhere.
The smaller of the source size and the destination size.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
18 people found this page helpful