GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::partition_copy(3) C++ Standard Libary std::partition_copy(3)

std::partition_copy - std::partition_copy


Defined in header <algorithm>
template< class InputIt, class OutputIt1,


class OutputIt2, class UnaryPredicate >
std::pair<OutputIt1, OutputIt2> (since C++11)
partition_copy( InputIt first, InputIt last, (until C++20)
OutputIt1 d_first_true, OutputIt2 d_first_false,


UnaryPredicate p );
template< class InputIt, class OutputIt1,


class OutputIt2, class UnaryPredicate >
constexpr std::pair<OutputIt1, OutputIt2> (since C++20)
partition_copy( InputIt first, InputIt last, (1)
OutputIt1 d_first_true, OutputIt2 d_first_false,


UnaryPredicate p );
template< class ExecutionPolicy, class ForwardIt1, class
ForwardIt2,


class ForwardIt3, class UnaryPredicate >
std::pair<ForwardIt2, ForwardIt3> (2) (since C++17)
partition_copy( ExecutionPolicy&& policy, ForwardIt1 first,
ForwardIt1 last,
ForwardIt2 d_first_true, ForwardIt3 d_first_false,


UnaryPredicate p );


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>>
(until C++20)
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>
(since C++20) is true.


first, last - the range of elements to copy from
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.
unary predicate which returns true if the element should be
placed in d_first_true.


The expression p(v) must be convertible to bool for every argument v
p - of type (possibly const) VT, where VT is the value type of InputIt,
regardless of value category, and must not modify v. Thus, a
parameter type of VT&is not allowed
, nor is VT unless for VT a move is equivalent to a copy
(since C++11).


-
InputIt must meet the requirements of LegacyInputIterator.
-
The type of dereferenced InputIt must meet the requirements of CopyAssignable.
-
OutputIt1, OutputIt2 must meet the requirements of LegacyOutputIterator.
-
ForwardIt1, ForwardIt2, ForwardIt3 must meet the requirements of
LegacyForwardIterator. ForwardIt1's value type must be CopyAssignable, writable to
ForwardIt2 and ForwardIt3, and convertible to UnaryPredicate's argument type
-
UnaryPredicate must meet the requirements of Predicate.


A std::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.


Exactly distance(first, last) applications of p.


For the overload with an ExecutionPolicy, there may be a performance cost if
ForwardIt's value type is not CopyConstructible.


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
and ExecutionPolicy is one of the standard policies, std::terminate is called.
For any other ExecutionPolicy, the behavior is implementation-defined.
* If the algorithm fails to allocate memory, std::bad_alloc is thrown.


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);
}

// Run this code


#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 (int x : true_arr) {
std::cout << x << ' ';
}
std::cout << '\n';


std::cout << "false_arr: ";
for (int x : false_arr) {
std::cout << x << ' ';
}
std::cout << '\n';


return 0;


}


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


partition divides a range of elements into two groups
(function template)
divides elements into two groups while preserving their
stable_partition relative order
(function template)
copy copies a range of elements to a new location
copy_if (function template)
(C++11)
remove_copy copies a range of elements omitting those that satisfy
remove_copy_if specific criteria
(function template)
ranges::partition_copy copies a range dividing the elements into two groups
(C++20) (niebloid)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.