Template Function thrust::partition(ForwardIterator, ForwardIterator, Predicate)¶
Function Documentation¶
-
template<typename
ForwardIterator, typenamePredicate>
ForwardIteratorthrust::partition(ForwardIterator first, ForwardIterator last, Predicate pred) partitionreorders the elements[first, last)based on the function objectpred, such that all of the elements that satisfypredprecede the elements that fail to satisfy it. The postcondition is that, for some iteratormiddlein the range[first, last),pred(*i)istruefor every iteratoriin the range[first,middle)andfalsefor every iteratoriin the range[middle, last). The return value ofpartitionismiddle.Note that the relative order of elements in the two reordered sequences is not necessarily the same as it was in the original sequence. A different algorithm, stable_partition, does guarantee to preserve the relative order.
The following code snippet demonstrates how to use
partitionto reorder a sequence so that even numbers precede odd numbers.- Return
An iterator referring to the first element of the second partition, that is, the sequence of the elements which do not satisfy
pred.- Parameters
first: The beginning of the sequence to reorder.last: The end of the sequence to reorder.pred: A function object which decides to which partition each element of the sequence[first, last)belongs.
- Template Parameters
ForwardIterator: is a model of Forward Iterator, andForwardIterator'svalue_typeis convertible toPredicate'sargument_type, andForwardIteratoris mutable.Predicate: is a model of Predicate.
#include <thrust/partition.h> ... struct is_even { __host__ __device__ bool operator()(const int &x) { return (x % 2) == 0; } }; ... int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; const int N = sizeof(A)/sizeof(int); thrust::partition(A, A + N, is_even()); // A is now {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}
- See
- See
stable_partition- See
partition_copy