C++ std::partial_sort_copy() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

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.

C++
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);
CallWhat 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
C++
template<class InputIt, class RandomIt>
RandomIt partial_sort_copy(InputIt first, InputIt last,
                           RandomIt d_first, RandomIt d_last);

Parameters

ParameterDescription
first, lastRequired. The source range.
d_first, d_lastRequired. The destination range. Its size decides how many elements are copied.
compOptional. A comparison function.

Return Value

Return ValueDescription
IteratorPoints 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.

C++
#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;
}

How It Works

  1. dest has room for three elements, so three are copied.
  2. They are the smallest values, sorted: 1, 4 and 7.
  3. src is printed unchanged.

Example 2 — Destination bigger than source

All source values are copied and sorted.

C++
#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

  1. The source has only three elements, so only three are written.
  2. The returned iterator shows that three slots were used.
  3. The last two slots of dest keep their original 0 values.

Example 3 — Two largest

Use std::greater to copy the biggest values.

C++
#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

  1. std::greater<int>() makes larger numbers count as "first".
  2. The two largest values are 80 and 50, in that order.
  3. 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.

Next: partition()

Reorders a range so elements that satisfy a condition come first.

partition() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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