std::partition (3) - Linux Manuals
std::partition: std::partition
NAME
std::partition - std::partition
Synopsis
Defined in header <algorithm>
template< class BidirIt, class UnaryPredicate > (until C++11)
BidirIt partition( BidirIt first, BidirIt last, UnaryPredicate p );
template< class ForwardIt, class UnaryPredicate > (since C++11)
ForwardIt partition( ForwardIt first, ForwardIt last, UnaryPredicate p ); (until C++20)
template< class ForwardIt, class UnaryPredicate > (1) (since C++20)
constexpr ForwardIt partition( ForwardIt first, ForwardIt last, UnaryPredicate p );
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
ForwardIt partition( ExecutionPolicy&& policy, (2) (since C++17)
ForwardIt first, ForwardIt last, UnaryPredicate p );
1) Reorders the elements in the range [first, last) in such a way that all elements for which the predicate p returns true precede the elements for which predicate p returns false. Relative order of the elements is not preserved.
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
Parameters
first, last - the range of elements to reorder
policy - the execution policy to use. See execution_policy for details.
p - The expression p(v) must be convertible to bool for every argument v of type (possibly const) VT, where VT is the value type of ForwardIt, regardless of value_category, and must not modify v. Thus, a parameter type of VT&is not allowed
Type requirements
-
BidirIt must meet the requirements of LegacyBidirectionalIterator.
-
ForwardIt must meet the requirements of ValueSwappable and LegacyForwardIterator. However, the operation is more efficient if ForwardIt also satisfies the requirements of LegacyBidirectionalIterator
-
UnaryPredicate must meet the requirements of Predicate.
Return value
Iterator to the first element of the second group.
Complexity
Given N = std::distance(first,last),
1) Exactly N applications of the predicate. At most N/2 swaps if ForwardIt meets the requirements of LegacyBidirectionalIterator, and at most N swaps otherwise.
2) O(N log N) swaps and O(N) applications of the predicate.
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 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.
Possible implementation
Example
// Run this code