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);
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
Parameter
Description
first, last
Required. The source range.
d_first_true
Required. Where elements that pass the predicate are written.
d_first_false
Required. Where elements that fail the predicate are written.
p
Required. A predicate.
Return Value
Return Value
Description
std::pair
The 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;
}
Output
Even: 2 4 6
Odd: 1 3 5
How It Works
The first back inserter receives numbers that pass the test, the second receives the others.
Each number is copied once into exactly one of the two vectors.
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;
}
Output
Long: tree river
Short: sun sky
How It Works
Words with four or more letters go into longWords.
sun and sky have three letters, so they go to shortWords.
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;
}
Output
Non-negative: 3, Negative: 2
How It Works
Plain arrays work because pointers are iterators.
5, 8 and 0 go to pos. -2 and -7 go to neg.
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.