function template
<algorithm>

std::partition_copy

template <class InputIterator, class OutputIterator1,          class OutputIterator2, class UnaryPredicate pred>  pair<OutputIterator1,OutputIterator2>    partition_copy (InputIterator first, InputIterator last,                    OutputIterator1 result_true, OutputIterator2 result_false,                    UnaryPredicate pred);
Partition range into two
Copies the elements in the range [first,last) for which pred returns true into the range pointed by result_true, and those for which it does not into the range pointed by result_false.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
template <class InputIterator, class OutputIterator1,
          class OutputIterator2, class UnaryPredicate pred>
  pair<OutputIterator1,OutputIterator2>
    partition_copy (InputIterator first, InputIterator last,
                    OutputIterator1 result_true, OutputIterator2 result_false,
                    UnaryPredicate pred)
{
  while (first!=last) {
    if (pred(*first)) {
      *result_true = *first;
      ++result_true;
    }
    else {
      *result_false = *first;
      ++result_false;
    }
    ++first;
  }
  return std::make_pair (result_true,result_false);
}

Parameters

first, last
Input iterators to the initial and final positions of the range to be copy-partitioned. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
result_true
Output iterator to the initial position of the range where the elements for which pred returns true are stored.
result_false
Output iterator to the initial position of the range where the elements for which pred returns false are stored.
pred
Unary function that accepts an element pointed by InputIterator as argument, and returns a value convertible to bool. The value returned indicates on which result range the element is copied.
The function shall not modify its argument.
This can either be a function pointer or a function object.

The ranges shall not overlap.
The type pointed by the output iterator types shall support being assigned the value of an element in the range [first,last).

Return value

A pair of iterators with the end of the generated sequences pointed by result_true and result_false, respectivelly.

Its member first points to the element that follows the last element copied to the sequence of elements for which pred returned true.
Its member second points to the element that follows the last element copied to the sequence of elements for which pred returned false.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// partition_copy example
#include <iostream>     // std::cout
#include <algorithm>    // std::partition_copy, std::count_if
#include <vector>       // std::vector

bool IsOdd (int i) { return (i%2)==1; }

int main () {
  std::vector<int> foo {1,2,3,4,5,6,7,8,9};
  std::vector<int> odd, even;

  // resize vectors to proper size:
  unsigned n = std::count_if (foo.begin(), foo.end(), IsOdd);
  odd.resize(n); even.resize(foo.size()-n);

  // partition:
  std::partition_copy (foo.begin(), foo.end(), odd.begin(), even.begin(), IsOdd);

  // print contents:
  std::cout << "odd: ";  for (int& x:odd)  std::cout << ' ' << x; std::cout << '\n';
  std::cout << "even: "; for (int& x:even) std::cout << ' ' << x; std::cout << '\n';

  return 0;
}

Output:
odd: 1 3 5 7 9
even: 2 4 6 8


Complexity

Linear in the distance between first and last: Calls pred and performs an assignment once for each element.

Data races

The objects in the range [first,last) are accessed (each exactly once).
The objects in the ranges pointed by result_true and result_false up to the elements pointed by the iterators returned are modified (each exactly once).

Exceptions

Throws if any of pred, the element assignments or the operations on iterators throws.
Note that invalid arguments cause undefined behavior.

See also