thrust

◆ swap_ranges() [2/2]

template<typename ForwardIterator1 , typename ForwardIterator2 >
ForwardIterator2 thrust::swap_ranges ( 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).

Parameters
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
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.

#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(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