# Copying

## Groups

` template <typename DerivedPolicy, typename InputIterator, typename OutputIterator> __host__ __device__ OutputIterator `

**thrust::copy**(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, InputIterator first, InputIterator last, OutputIterator result);

template <typename DerivedPolicy, typename InputIterator, typename Size, typename OutputIterator> __host__ __device__ OutputIterator **thrust::copy_n**(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, InputIterator first, Size n, OutputIterator result);

template <typename InputIterator, typename OutputIterator> OutputIterator **thrust::copy**(InputIterator first, InputIterator last, OutputIterator result);

template <typename InputIterator, typename Size, typename OutputIterator> OutputIterator **thrust::copy_n**(InputIterator first, Size n, OutputIterator result);

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

template <typename ForwardIterator1, typename ForwardIterator2> ForwardIterator2 **thrust::swap_ranges**(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);

template <typename DerivedPolicy, typename InputIterator, typename ForwardIterator> __host__ __device__ ForwardIterator **thrust::uninitialized_copy**(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, InputIterator first, InputIterator last, ForwardIterator result);

template <typename InputIterator, typename ForwardIterator> ForwardIterator **thrust::uninitialized_copy**(InputIterator first, InputIterator last, ForwardIterator result);

template <typename DerivedPolicy, typename InputIterator, typename Size, typename ForwardIterator> __host__ __device__ ForwardIterator **thrust::uninitialized_copy_n**(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, InputIterator first, Size n, ForwardIterator result);

template <typename InputIterator, typename Size, typename ForwardIterator> ForwardIterator **thrust::uninitialized_copy_n**(InputIterator first, Size n, ForwardIterator result);

## Functions

### Function `thrust::copy`

` template <typename DerivedPolicy, typename InputIterator, typename OutputIterator> __host__ __device__ OutputIterator `

**copy**(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, InputIterator first, InputIterator last, OutputIterator result);`copy`

copies elements from the range [`first`

, `last`

) to the range [`result`

, `result`

+ (`last`

- `first`

)). That is, it performs the assignments *`result`

= *`first`

, *(`result`

+ `1`

) = *(`first`

+ `1`

), and so on. Generally, for every integer `n`

from `0`

to `last`

- `first`

, `copy`

performs the assignment *(`result`

+ `n`

) = *(`first`

+ `n`

). Unlike `std::copy`

, `copy`

offers no guarantee on order of operation. As a result, calling `copy`

with overlapping source and destination ranges has undefined behavior.

The return value is `result`

+ (`last`

- `first`

).

The algorithm’s execution is parallelized as determined by `exec`

.

The following code snippet demonstrates how to use `copy`

to copy from one range to another using the `thrust::device`

parallelization policy:

```
#include <thrust/copy.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
thrust::device_vector<int> vec0(100);
thrust::device_vector<int> vec1(100);
...
thrust::copy(thrust::device, vec0.begin(), vec0.end(), vec1.begin());
// vec1 is now a copy of vec0
```

**Template Parameters**:

The name of the derived execution policy.`DerivedPolicy`

must be a model of Input Iterator and`InputIterator`

`InputIterator's`

`value_type`

must be convertible to`OutputIterator's`

`value_type`

.must be a model of Output Iterator.`OutputIterator`

**Function Parameters**:

The execution policy to use for parallelization.`exec`

The beginning of the sequence to copy.`first`

The end of the sequence to copy.`last`

The destination sequence.`result`

**Preconditions**: `result`

may be equal to `first`

, but `result`

shall not be in the range `[first, last)`

otherwise.

**Returns**: The end of the destination sequence.

**See**: https://en.cppreference.com/w/cpp/algorithm/copy

### Function `thrust::copy_n`

` template <typename DerivedPolicy, typename InputIterator, typename Size, typename OutputIterator> __host__ __device__ OutputIterator `

**copy_n**(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, InputIterator first, Size n, OutputIterator result);`copy_n`

copies elements from the range `[first, first + n)`

to the range `[result, result + n)`

. That is, it performs the assignments `*result = *first, *(result + 1) = *(first + 1)`

, and so on. Generally, for every integer `i`

from `0`

to `n`

, `copy`

performs the assignment *(`result`

+ `i`

) = *(`first`

+ `i`

). Unlike `std::copy_n`

, `copy_n`

offers no guarantee on order of operation. As a result, calling `copy_n`

with overlapping source and destination ranges has undefined behavior.

The return value is `result`

+ `n`

.

The algorithm’s execution is parallelized as determined by `exec`

.

The following code snippet demonstrates how to use `copy`

to copy from one range to another using the `thrust::device`

parallelization policy:

```
#include <thrust/copy.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
...
size_t n = 100;
thrust::device_vector<int> vec0(n);
thrust::device_vector<int> vec1(n);
...
thrust::copy_n(thrust::device, vec0.begin(), n, vec1.begin());
// vec1 is now a copy of vec0
```

**Template Parameters**:

The name of the derived execution policy.`DerivedPolicy`

must be a model of Input Iterator and`InputIterator`

`InputIterator's`

`value_type`

must be convertible to`OutputIterator's`

`value_type`

.is an integral type.`Size`

must be a model of Output Iterator.`OutputIterator`

**Function Parameters**:

The execution policy to use for parallelization.`exec`

The beginning of the range to copy.`first`

The number of elements to copy.`n`

The beginning destination range.`result`

**Preconditions**: `result`

may be equal to `first`

, but `result`

shall not be in the range `[first, first + n)`

otherwise.

**Returns**: The end of the destination range.

**See**:

- https://en.cppreference.com/w/cpp/algorithm/copy_n
- thrust::copy

### Function `thrust::copy`

` template <typename InputIterator, typename OutputIterator> OutputIterator `

**copy**(InputIterator first, InputIterator last, OutputIterator result);`copy`

copies elements from the range [`first`

, `last`

) to the range [`result`

, `result`

+ (`last`

- `first`

)). That is, it performs the assignments *`result`

= *`first`

, *(`result`

+ `1`

) = *(`first`

+ `1`

), and so on. Generally, for every integer `n`

from `0`

to `last`

- `first`

, `copy`

performs the assignment *(`result`

+ `n`

) = *(`first`

+ `n`

). Unlike `std::copy`

, `copy`

offers no guarantee on order of operation. As a result, calling `copy`

with overlapping source and destination ranges has undefined behavior.

The return value is `result`

+ (`last`

- `first`

).

The following code snippet demonstrates how to use `copy`

to copy from one range to another.

```
#include <thrust/copy.h>
#include <thrust/device_vector.h>
...
thrust::device_vector<int> vec0(100);
thrust::device_vector<int> vec1(100);
...
thrust::copy(vec0.begin(), vec0.end(),
vec1.begin());
// vec1 is now a copy of vec0
```

**Template Parameters**:

must be a model of Input Iterator and`InputIterator`

`InputIterator's`

`value_type`

must be convertible to`OutputIterator's`

`value_type`

.must be a model of Output Iterator.`OutputIterator`

**Function Parameters**:

The beginning of the sequence to copy.`first`

The end of the sequence to copy.`last`

The destination sequence.`result`

**Preconditions**: `result`

may be equal to `first`

, but `result`

shall not be in the range `[first, last)`

otherwise.

**Returns**: The end of the destination sequence.

**See**: https://en.cppreference.com/w/cpp/algorithm/copy

### Function `thrust::copy_n`

` template <typename InputIterator, typename Size, typename OutputIterator> OutputIterator `

**copy_n**(InputIterator first, Size n, OutputIterator result);`copy_n`

copies elements from the range `[first, first + n)`

to the range `[result, result + n)`

. That is, it performs the assignments `*result = *first, *(result + 1) = *(first + 1)`

, and so on. Generally, for every integer `i`

from `0`

to `n`

, `copy`

performs the assignment *(`result`

+ `i`

) = *(`first`

+ `i`

). Unlike `std::copy_n`

, `copy_n`

offers no guarantee on order of operation. As a result, calling `copy_n`

with overlapping source and destination ranges has undefined behavior.

The return value is `result`

+ `n`

.

The following code snippet demonstrates how to use `copy`

to copy from one range to another.

```
#include <thrust/copy.h>
#include <thrust/device_vector.h>
...
size_t n = 100;
thrust::device_vector<int> vec0(n);
thrust::device_vector<int> vec1(n);
...
thrust::copy_n(vec0.begin(), n, vec1.begin());
// vec1 is now a copy of vec0
```

**Template Parameters**:

must be a model of Input Iterator and`InputIterator`

`InputIterator's`

`value_type`

must be convertible to`OutputIterator's`

`value_type`

.is an integral type.`Size`

must be a model of Output Iterator.`OutputIterator`

**Function Parameters**:

The beginning of the range to copy.`first`

The number of elements to copy.`n`

The beginning destination range.`result`

**Preconditions**: `result`

may be equal to `first`

, but `result`

shall not be in the range `[first, first + n)`

otherwise.

**Returns**: The end of the destination range.

**See**:

- https://en.cppreference.com/w/cpp/algorithm/copy_n
- thrust::copy

### Function `thrust::swap_ranges`

` template <typename DerivedPolicy, typename ForwardIterator1, typename ForwardIterator2> __host__ __device__ ForwardIterator2 `

**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`

.

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

**Template Parameters**:

The name of the derived execution policy.`DerivedPolicy`

is a model of Forward Iterator, and`ForwardIterator1`

`ForwardIterator1's`

`value_type`

must be convertible to`ForwardIterator2's`

`value_type`

.is a model of Forward Iterator, and`ForwardIterator2`

`ForwardIterator2's`

`value_type`

must be convertible to`ForwardIterator1's`

`value_type`

.

**Function Parameters**:

The execution policy to use for parallelization.`exec`

The beginning of the first sequence to swap.`first1`

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

The beginning of the second sequence to swap.`first2`

**Preconditions**: `first1`

may equal `first2`

, but the range `[first1, last1)`

shall not overlap the range `[first2, first2 + (last1 - first1))`

otherwise.

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

**See**:

### Function `thrust::swap_ranges`

` template <typename ForwardIterator1, typename ForwardIterator2> ForwardIterator2 `

**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
```

**Template Parameters**:

is a model of Forward Iterator, and`ForwardIterator1`

`ForwardIterator1's`

`value_type`

must be convertible to`ForwardIterator2's`

`value_type`

.is a model of Forward Iterator, and`ForwardIterator2`

`ForwardIterator2's`

`value_type`

must be convertible to`ForwardIterator1's`

`value_type`

.

**Function Parameters**:

The beginning of the first sequence to swap.`first1`

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

The beginning of the second sequence to swap.`first2`

**Preconditions**: `first1`

may equal `first2`

, but the range `[first1, last1)`

shall not overlap the range `[first2, first2 + (last1 - first1))`

otherwise.

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

**See**:

### Function `thrust::uninitialized_copy`

` template <typename DerivedPolicy, typename InputIterator, typename ForwardIterator> __host__ __device__ ForwardIterator `

In **uninitialized_copy**(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, InputIterator first, InputIterator last, ForwardIterator result);`thrust`

, the function `thrust::device_new`

allocates memory for an object and then creates an object at that location by calling a constructor. Occasionally, however, it is useful to separate those two operations. If each iterator in the range `[result, result + (last - first))`

points to uninitialized memory, then `uninitialized_copy`

creates a copy of `[first, last)`

in that range. That is, for each iterator `i`

in the input, `uninitialized_copy`

creates a copy of `*i`

in the location pointed to by the corresponding iterator in the output range by `ForwardIterator's`

`value_type's`

copy constructor with *i as its argument.

The algorithm’s execution is parallelized as determined by `exec`

.

The following code snippet demonstrates how to use `uninitialized_copy`

to initialize a range of uninitialized memory using the `thrust::device`

execution policy for parallelization:

```
#include <thrust/uninitialized_copy.h>
#include <thrust/device_malloc.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
struct Int
{
__host__ __device__
Int(int x) : val(x) {}
int val;
};
...
const int N = 137;
Int val(46);
thrust::device_vector<Int> input(N, val);
thrust::device_ptr<Int> array = thrust::device_malloc<Int>(N);
thrust::uninitialized_copy(thrust::device, input.begin(), input.end(), array);
// Int x = array[i];
// x.val == 46 for all 0 <= i < N
```

**Template Parameters**:

The name of the derived execution policy.`DerivedPolicy`

is a model of Input Iterator.`InputIterator`

is a model of Forward Iterator,`ForwardIterator`

`ForwardIterator`

is mutable, and`ForwardIterator's`

`value_type`

has a constructor that takes a single argument whose type is`InputIterator's`

`value_type`

.

**Function Parameters**:

The execution policy to use for parallelization.`exec`

The first element of the input range to copy from.`first`

The last element of the input range to copy from.`last`

The first element of the output range to copy to.`result`

**Preconditions**: `first`

may equal `result`

, but the range `[first, last)`

and the range `[result, result + (last - first))`

shall not overlap otherwise.

**Returns**: An iterator pointing to the last element of the output range.

**See**:

- https://en.cppreference.com/w/cpp/memory/uninitialized_copy
`copy`

`uninitialized_fill`

`device_new`

`device_malloc`

### Function `thrust::uninitialized_copy`

` template <typename InputIterator, typename ForwardIterator> ForwardIterator `

In **uninitialized_copy**(InputIterator first, InputIterator last, ForwardIterator result);`thrust`

, the function `thrust::device_new`

allocates memory for an object and then creates an object at that location by calling a constructor. Occasionally, however, it is useful to separate those two operations. If each iterator in the range `[result, result + (last - first))`

points to uninitialized memory, then `uninitialized_copy`

creates a copy of `[first, last)`

in that range. That is, for each iterator `i`

in the input, `uninitialized_copy`

creates a copy of `*i`

in the location pointed to by the corresponding iterator in the output range by `ForwardIterator's`

`value_type's`

copy constructor with *i as its argument.

The following code snippet demonstrates how to use `uninitialized_copy`

to initialize a range of uninitialized memory.

```
#include <thrust/uninitialized_copy.h>
#include <thrust/device_malloc.h>
#include <thrust/device_vector.h>
struct Int
{
__host__ __device__
Int(int x) : val(x) {}
int val;
};
...
const int N = 137;
Int val(46);
thrust::device_vector<Int> input(N, val);
thrust::device_ptr<Int> array = thrust::device_malloc<Int>(N);
thrust::uninitialized_copy(input.begin(), input.end(), array);
// Int x = array[i];
// x.val == 46 for all 0 <= i < N
```

**Template Parameters**:

is a model of Input Iterator.`InputIterator`

is a model of Forward Iterator,`ForwardIterator`

`ForwardIterator`

is mutable, and`ForwardIterator's`

`value_type`

has a constructor that takes a single argument whose type is`InputIterator's`

`value_type`

.

**Function Parameters**:

The first element of the input range to copy from.`first`

The last element of the input range to copy from.`last`

The first element of the output range to copy to.`result`

**Preconditions**: `first`

may equal `result`

, but the range `[first, last)`

and the range `[result, result + (last - first))`

shall not overlap otherwise.

**Returns**: An iterator pointing to the last element of the output range.

**See**:

- https://en.cppreference.com/w/cpp/memory/uninitialized_copy
`copy`

`uninitialized_fill`

`device_new`

`device_malloc`

### Function `thrust::uninitialized_copy_n`

` template <typename DerivedPolicy, typename InputIterator, typename Size, typename ForwardIterator> __host__ __device__ ForwardIterator `

In **uninitialized_copy_n**(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, InputIterator first, Size n, ForwardIterator result);`thrust`

, the function `thrust::device_new`

allocates memory for an object and then creates an object at that location by calling a constructor. Occasionally, however, it is useful to separate those two operations. If each iterator in the range `[result, result + n)`

points to uninitialized memory, then `uninitialized_copy_n`

creates a copy of `[first, first + n)`

in that range. That is, for each iterator `i`

in the input, `uninitialized_copy_n`

creates a copy of `*i`

in the location pointed to by the corresponding iterator in the output range by `InputIterator's`

`value_type's`

copy constructor with *i as its argument.

The algorithm’s execution is parallelized as determined by `exec`

.

The following code snippet demonstrates how to use `uninitialized_copy`

to initialize a range of uninitialized memory using the `thrust::device`

execution policy for parallelization:

```
#include <thrust/uninitialized_copy.h>
#include <thrust/device_malloc.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
struct Int
{
__host__ __device__
Int(int x) : val(x) {}
int val;
};
...
const int N = 137;
Int val(46);
thrust::device_vector<Int> input(N, val);
thrust::device_ptr<Int> array = thrust::device_malloc<Int>(N);
thrust::uninitialized_copy_n(thrust::device, input.begin(), N, array);
// Int x = array[i];
// x.val == 46 for all 0 <= i < N
```

**Template Parameters**:

The name of the derived execution policy.`DerivedPolicy`

is a model of Input Iterator.`InputIterator`

is an integral type.`Size`

is a model of Forward Iterator,`ForwardIterator`

`ForwardIterator`

is mutable, and`ForwardIterator's`

`value_type`

has a constructor that takes a single argument whose type is`InputIterator's`

`value_type`

.

**Function Parameters**:

The execution policy to use for parallelization.`exec`

The first element of the input range to copy from.`first`

The number of elements to copy.`n`

The first element of the output range to copy to.`result`

**Preconditions**: `first`

may equal `result`

, but the range `[first, first + n)`

and the range `[result, result + n)`

shall not overlap otherwise.

**Returns**: An iterator pointing to the last element of the output range.

**See**:

- https://en.cppreference.com/w/cpp/memory/uninitialized_copy
`uninitialized_copy`

`copy`

`uninitialized_fill`

`device_new`

`device_malloc`

### Function `thrust::uninitialized_copy_n`

` template <typename InputIterator, typename Size, typename ForwardIterator> ForwardIterator `

In **uninitialized_copy_n**(InputIterator first, Size n, ForwardIterator result);`thrust`

, the function `thrust::device_new`

allocates memory for an object and then creates an object at that location by calling a constructor. Occasionally, however, it is useful to separate those two operations. If each iterator in the range `[result, result + n)`

points to uninitialized memory, then `uninitialized_copy_n`

creates a copy of `[first, first + n)`

in that range. That is, for each iterator `i`

in the input, `uninitialized_copy_n`

creates a copy of `*i`

in the location pointed to by the corresponding iterator in the output range by `InputIterator's`

`value_type's`

copy constructor with *i as its argument.

The following code snippet demonstrates how to use `uninitialized_copy`

to initialize a range of uninitialized memory.

```
#include <thrust/uninitialized_copy.h>
#include <thrust/device_malloc.h>
#include <thrust/device_vector.h>
struct Int
{
__host__ __device__
Int(int x) : val(x) {}
int val;
};
...
const int N = 137;
Int val(46);
thrust::device_vector<Int> input(N, val);
thrust::device_ptr<Int> array = thrust::device_malloc<Int>(N);
thrust::uninitialized_copy_n(input.begin(), N, array);
// Int x = array[i];
// x.val == 46 for all 0 <= i < N
```

**Template Parameters**:

is a model of Input Iterator.`InputIterator`

is an integral type.`Size`

is a model of Forward Iterator,`ForwardIterator`

`ForwardIterator`

is mutable, and`ForwardIterator's`

`value_type`

has a constructor that takes a single argument whose type is`InputIterator's`

`value_type`

.

**Function Parameters**:

The first element of the input range to copy from.`first`

The number of elements to copy.`n`

The first element of the output range to copy to.`result`

**Preconditions**: `first`

may equal `result`

, but the range `[first, first + n)`

and the range `[result, result + n)`

shall not overlap otherwise.

**Returns**: An iterator pointing to the last element of the output range.

**See**:

- https://en.cppreference.com/w/cpp/memory/uninitialized_copy
`uninitialized_copy`

`copy`

`uninitialized_fill`

`device_new`

`device_malloc`