std::partition_copy

From cppreference.com
< cpp‎ | algorithm
 
 
Algorithm library
Execution policies (C++17)
Non-modifying sequence operations
(C++11)(C++11)(C++11)
(C++17)
Modifying sequence operations
(C++11)
(C++11)
(C++11)
(C++11)

Operations on uninitialized storage
Partitioning operations
partition_copy
(C++11)
Sorting operations
(C++11)
(C++11)
Binary search operations
Set operations (on sorted ranges)
Heap operations
(C++11)
(C++11)
Minimum/maximum operations
(C++11)
(C++11)
(C++17)

Permutations
(C++11)
Numeric operations
C library
 
Defined in header <algorithm>
template< class InputIt, class OutputIt1,

          class OutputIt2, class UnaryPredicate >
std::pair<OutputIt1, OutputIt2>
     partition_copy( InputIt first, InputIt last,
                     OutputIt1 d_first_true, OutputIt2 d_first_false,

                     UnaryPredicate p );
(1) (since C++11)
template< class ExecutionPolicy, class InputIt, class OutputIt1,

          class OutputIt2, class UnaryPredicate >
std::pair<OutputIt1, OutputIt2>
     partition_copy( ExecutionPolicy&& policy, InputIt first, InputIt last,
                     OutputIt1 d_first_true, OutputIt2 d_first_false,

                     UnaryPredicate p );
(2) (since C++17)
1) Copies the elements from the range [first, last) to two different ranges depending on the value returned by the predicate p. The elements, that satisfy the predicate p, are copied to the range beginning at d_first_true. The rest of the elements are copied to the range beginning at d_first_false.
The behavior is undefined if the input range overlaps either of the output ranges.
2) Same as (1), but executed according to policy. This overload does not participate in overload resolution unless std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true

Contents

[edit] Parameters

first, last - the range of elements to sort
d_first_true - the beginning of the output range for the elements that satisfy p
d_first_false - the beginning of the output range for the elements that do not satisfy p
policy - the execution policy to use. See execution policy for details.
p - unary predicate which returns ​true if the element should be placed in d_first_true.

The signature of the predicate function should be equivalent to the following:

 bool pred(const Type &a);

The signature does not need to have const &, but the function must not modify the objects passed to it.
The type Type must be such that an object of type InputIt can be dereferenced and then implicitly converted to Type. ​

Type requirements
-
InputIt must meet the requirements of InputIterator.
-
The type of dereferenced InputIt must meet the requirements of CopyAssignable.
-
OutputIt1 must meet the requirements of OutputIterator.
-
OutputIt2 must meet the requirements of OutputIterator.
-
UnaryPredicate must meet the requirements of Predicate.

[edit] Return value

A pair constructed from the iterator to the end of the d_first_true range and the iterator to the end of the d_first_false range.

[edit] Complexity

Exactly distance(first, last) applications of p.

[edit] Exceptions

The overload with a template parameter named ExecutionPolicy reports errors as follows:

  • If execution of a function invoked as part of the algorithm throws an exception,
  • if policy is std::parallel_vector_execution_policy, std::terminate is called
  • if policy is std::sequential_execution_policy or std::parallel_execution_policy, the algorithm exits with an std::exception_list containing all uncaught exceptions. If there was only one uncaught exception, the algorithm may rethrow it without wrapping in std::exception_list. It is unspecified how much work the algorithm will perform before returning after the first exception was encountered.
  • if policy is some other type, the behavior is implementation-defined
  • If the algorithm fails to allocate memory (either for itself or to construct an std::exception_list when handling a user exception), std::bad_alloc is thrown.

[edit] Possible implementation

template<class InputIt, class OutputIt1,
         class OutputIt2, class UnaryPredicate>
std::pair<OutputIt1, OutputIt2>
    partition_copy(InputIt first, InputIt last,
                   OutputIt1 d_first_true, OutputIt2 d_first_false,
                   UnaryPredicate p)
{
    while (first != last) {
        if (p(*first)) {
            *d_first_true = *first;
            ++d_first_true;
        } else {
            *d_first_false = *first;
            ++d_first_false;
        }
        ++first;
    }
    return std::pair<OutputIt1, OutputIt2>(d_first_true, d_first_false);
}

[edit] Example

#include <iostream>
#include <algorithm>
#include <utility>
 
int main()
{
    int arr [10] = {1,2,3,4,5,6,7,8,9,10};
    int true_arr [5] = {0};
    int false_arr [5] = {0};
 
    std::partition_copy(std::begin(arr), std::end(arr), std::begin(true_arr),std::begin(false_arr),
                        [] (int i) {return i > 5;});
 
    std::cout << "true_arr: ";
    for (auto it = std::begin(true_arr); it != std::end(true_arr); ++it) {
        std::cout << *it << ' ';
    }
    std::cout << '\n'; 
 
    std::cout << "false_arr: ";
    for (auto it = std::begin(false_arr); it != std::end(false_arr); ++it) {
        std::cout << *it << ' ';
    }
    std::cout << '\n'; 
 
    return 0;
 
}

Output:

true_arr: 6 7 8 9 10
false_arr: 1 2 3 4 5

[edit] See also

divides a range of elements into two groups
(function template)
divides elements into two groups while preserving their relative order
(function template)
parallelized version of std::partition_copy
(function template)