C++ std::partition_copy() Function

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

Definition

In C++, std::partition_copy() reads a range and sends each element to one of two outputs. Elements for which the predicate is true go to the first output, and the others go to the second.

The source is not changed, and the order of elements in each output matches the source.

Syntax

Add #include <algorithm>, then call std::partition_copy(). Start with these two lines.

C++
std::partition_copy(src.begin(), src.end(), std::back_inserter(yes), std::back_inserter(no), pred);
auto r = std::partition_copy(a, a + n, out1, out2, pred);
CallWhat it does
std::partition_copy(src.begin(), src.end(), std::back_inserter(yes), std::back_inserter(no), pred);Split into two vectors.
auto r = std::partition_copy(a, a + n, out1, out2, pred);Use plain arrays and read both end pointers.
Library declarations
C++
template<class InputIt, class OutputIt1, class OutputIt2, class UnaryPred>
std::pair<OutputIt1, OutputIt2>
    partition_copy(InputIt first, InputIt last,
                   OutputIt1 d_first_true, OutputIt2 d_first_false,
                   UnaryPred p);

Parameters

ParameterDescription
first, lastRequired. The source range.
d_first_trueRequired. Where elements that pass the predicate are written.
d_first_falseRequired. Where elements that fail the predicate are written.
pRequired. A predicate.

Return Value

Return ValueDescription
std::pairThe end of each output: first for the true output and second for the false output.

C++ Examples

Example 1 — Even and odd vectors

Split numbers into two new vectors.

C++
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main() {
    std::vector<int> src = {1, 2, 3, 4, 5, 6};
    std::vector<int> even;
    std::vector<int> odd;

    std::partition_copy(src.begin(), src.end(),
                        std::back_inserter(even), std::back_inserter(odd),
                        [](int n) { return n % 2 == 0; });

    std::cout << "Even: ";
    for (int x : even) std::cout << x << ' ';
    std::cout << '\n';
    std::cout << "Odd: ";
    for (int x : odd) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. The first back inserter receives numbers that pass the test, the second receives the others.
  2. Each number is copied once into exactly one of the two vectors.
  3. The order inside each vector follows the source.

Example 2 — Long and short words

Split strings by their length.

C++
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> words = {"tree", "sun", "river", "sky"};
    std::vector<std::string> longWords;
    std::vector<std::string> shortWords;

    std::partition_copy(words.begin(), words.end(),
                        std::back_inserter(longWords), std::back_inserter(shortWords),
                        [](const std::string& w) { return w.size() >= 4; });

    std::cout << "Long: ";
    for (const std::string& x : longWords) std::cout << x << ' ';
    std::cout << '\n';
    std::cout << "Short: ";
    for (const std::string& x : shortWords) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. Words with four or more letters go into longWords.
  2. sun and sky have three letters, so they go to shortWords.
  3. The source vector is untouched.

Example 3 — Arrays and counts

Use the returned pointers to count each group.

C++
#include <algorithm>
#include <iostream>

int main() {
    int src[] = {5, -2, 8, -7, 0};
    int pos[5];
    int neg[5];

    auto r = std::partition_copy(src, src + 5, pos, neg, [](int n) { return n >= 0; });

    std::cout << "Non-negative: " << (r.first - pos)
              << ", Negative: " << (r.second - neg) << '\n';
    return 0;
}

How It Works

  1. Plain arrays work because pointers are iterators.
  2. 5, 8 and 0 go to pos. -2 and -7 go to neg.
  3. Subtracting the start pointer from each end pointer gives the size of each group.

Frequently Asked Questions

In C++, std::partition_copy() copies elements into two destinations depending on a condition. Include <algorithm> to use it.
Include <algorithm>. It needs C++11 or newer, so compile with -std=c++11 or later.
It returns a pair of output iterators, one after the last element written to each destination.
partition() rearranges the original range. partition_copy() leaves it alone and writes two new groups.
Yes. Each output receives the elements in the same order as the source.

Next: partition_point()

Finds the border of a partitioned range.

partition_point() 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