thrust

◆ partition() [2/4]

template<typename ForwardIterator , typename Predicate >
ForwardIterator thrust::partition ( ForwardIterator  first,
ForwardIterator  last,
Predicate  pred 
)

partition reorders the elements [first, last) based on the function object pred, such that all of the elements that satisfy pred precede the elements that fail to satisfy it. The postcondition is that, for some iterator middle in the range [first, last), pred(*i) is true for every iterator i in the range [first,middle) and false for every iterator i in the range [middle, last). The return value of partition is middle.

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.

Parameters
firstThe beginning of the sequence to reorder.
lastThe end of the sequence to reorder.
predA function object which decides to which partition each element of the sequence [first, last) belongs.
Returns
An iterator referring to the first element of the second partition, that is, the sequence of the elements which do not satisfy pred.
Template Parameters
ForwardIteratoris a model of Forward Iterator, and ForwardIterator's value_type is convertible to Predicate's argument_type, and ForwardIterator is mutable.
Predicateis a model of Predicate.

The following code snippet demonstrates how to use partition to reorder a sequence so that even numbers precede odd numbers.

...
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);
is_even());
// A is now {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}
See also
http://www.sgi.com/tech/stl/partition.html
stable_partition
partition_copy