thrust

◆ swap_ranges() [1/2]

template<typename DerivedPolicy , typename ForwardIterator1 , typename ForwardIterator2 >
__host__ __device__ ForwardIterator2 thrust::swap_ranges ( const thrust::detail::execution_policy_base< DerivedPolicy > &  exec,
ForwardIterator1  first1,
ForwardIterator1  last1,
ForwardIterator2  first2 
)

swap_ranges swaps each of the elements in the range [first1, last1) with the corresponding element in the range [first2, first2 + (last1 - first1)). That is, for each integer n such that 0 <= n < (last1 - first1), it swaps *(first1 + n) and *(first2 + n). The return value is first2 + (last1 - first1).

The algorithm's execution is parallelized as determined by exec.

Parameters
execThe execution policy to use for parallelization.
first1The beginning of the first sequence to swap.
last1One position past the last element of the first sequence to swap.
first2The beginning of the second sequence to swap.
Returns
An iterator pointing to one position past the last element of the second sequence to swap.
Template Parameters
DerivedPolicyThe name of the derived execution policy.
ForwardIterator1is a model of Forward Iterator, and ForwardIterator1's value_type must be convertible to ForwardIterator2's value_type.
ForwardIterator2is a model of Forward Iterator, and ForwardIterator2's value_type must be convertible to ForwardIterator1's value_type.
Precondition
first1 may equal first2, but the range [first1, last1) shall not overlap the range [first2, first2 + (last1 - first1)) otherwise.

The following code snippet demonstrates how to use swap_ranges to swap the contents of two thrust::device_vectors using the thrust::device execution policy for parallelization:

#include <thrust/swap.h>
...
thrust::device_vector<int> v1(2), v2(2);
v1[0] = 1;
v1[1] = 2;
v2[0] = 3;
v2[1] = 4;
thrust::swap_ranges(thrust::device, v1.begin(), v1.end(), v2.begin());
// v1[0] == 3, v1[1] == 4, v2[0] == 1, v2[1] == 2
See also
http://www.sgi.com/tech/stl/swap_ranges.html
swap