Template Function thrust::stable_partition_copy(InputIterator1, InputIterator1, InputIterator2, OutputIterator1, OutputIterator2, Predicate)¶
Function Documentation¶
-
template<typename
InputIterator1, typenameInputIterator2, typenameOutputIterator1, typenameOutputIterator2, typenamePredicate>
thrust::pair<OutputIterator1, OutputIterator2>thrust::stable_partition_copy(InputIterator1 first, InputIterator1 last, InputIterator2 stencil, OutputIterator1 out_true, OutputIterator2 out_false, Predicate pred) stable_partition_copydiffers from stable_partition only in that the reordered sequence is written to different output sequences, rather than in place.stable_partition_copycopies the elements[first, last)based on the function objectpredwhich is applied to a range of stencil elements. All of the elements whose corresponding stencil element satisfiespredare copied to the range beginning atout_trueand all the elements whose stencil element fails to satisfy it are copied to the range beginning atout_false.stable_partition_copydiffers from partition_copy in thatstable_partition_copyis guaranteed to preserve relative order. That is, ifxandyare elements in[first, last), such thatpred(x) == pred(y), and ifxprecedesy, then it will still be true afterstable_partition_copythatxprecedesyin the output.The following code snippet demonstrates how to use
stable_partition_copyto reorder a sequence so that even numbers precede odd numbers.- Return
A
pairp such thatp.firstis the end of the output range beginning atout_trueandp.secondis the end of the output range beginning atout_false.- Pre
The input ranges shall not overlap with either output range.
- Parameters
first: The first element of the sequence to reorder.last: One position past the last element of the sequence to reorder.stencil: The beginning of the stencil sequence.out_true: The destination of the resulting sequence of elements which satisfypred.out_false: The destination of the resulting sequence of elements which fail to satisfypred.pred: A function object which decides to which partition each element of the sequence[first, last)belongs.
- Template Parameters
InputIterator1: is a model of Input Iterator, andInputIterator'svalue_typeis convertible toOutputIterator1andOutputIterator2'svalue_types.InputIterator2: is a model of Input Iterator, andInputIterator2'svalue_typeis convertible toPredicate'sargument_type.OutputIterator1: is a model of Output Iterator.OutputIterator2: is a model of Output Iterator.Predicate: is a model of Predicate.
#include <thrust/partition.h> #include <thrust/functional.h> ... int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int S[] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1}; int result[10]; const int N = sizeof(A)/sizeof(int); int *evens = result; int *odds = result + 5; thrust::stable_partition_copy(A, A + N, S, evens, odds, thrust::identity<int>()); // A remains {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} // S remains {0, 1, 0, 1, 0, 1, 0, 1, 0, 1} // result is now {2, 4, 6, 8, 10, 1, 3, 5, 7, 9} // evens points to {2, 4, 6, 8, 10} // odds points to {1, 3, 5, 7, 9}
- See
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2569.pdf
- See
partition_copy- See
stable_partition