thrust::swap_ranges

Defined in thrust/swap.h

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

The following code snippet demonstrates how to use swap_ranges to swap the contents of two thrust::device_vectors.

#include <thrust/swap.h>
#include <thrust/device_vector.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

Swap

Parameters
  • first1 – The beginning of the first sequence to swap.

  • last1 – One position past the last element of the first sequence to swap.

  • first2 – The beginning of the second sequence to swap.

Template Parameters
  • ForwardIterator1 – is a model of Forward Iterator, and ForwardIterator1's value_type must be convertible to ForwardIterator2's value_type.

  • ForwardIterator2 – is a model of Forward Iterator, and ForwardIterator2's value_type must be convertible to ForwardIterator1's value_type.

Returns

An iterator pointing to one position past the last element of the second sequence to swap.

Pre

first1 may equal first2, but the range [first1, last1) shall not overlap the range [first2, first2 + (last1 - first1)) otherwise.