View source code
Display the source code in std/algorithm/mutation.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.algorithm.mutation.copy

Copies the content of source into target and returns the remaining (unfilled) part of target.

TargetRange copy(SourceRange, TargetRange) (
  SourceRange source,
  TargetRange target
)
if (isInputRange!SourceRange && isOutputRange!(TargetRange, ElementType!SourceRange));

Preconditions

target shall have enough room to accommodate the entirety of source.

Parameters

NameDescription
source an input range
target an output range

Returns

The unfilled part of target

Example

int[] a = [ 1, 5 ];
int[] b = [ 9, 8 ];
int[] buf = new int[](a.length + b.length + 10);
auto rem = a.copy(buf);    // copy a into buf
rem = b.copy(rem);         // copy b into remainder of buf
writeln(buf[0 .. a.length + b.length]); // [1, 5, 9, 8]
assert(rem.length == 10);   // unused slots in buf

Example

As long as the target range elements support assignment from source range elements, different types of ranges are accepted:

float[] src = [ 1.0f, 5 ];
double[] dest = new double[src.length];
src.copy(dest);

Example

To copy at most n elements from a range, you may want to use std.range.take:

import std.range;
int[] src = [ 1, 5, 8, 9, 10 ];
auto dest = new int[](3);
src.take(dest.length).copy(dest);
writeln(dest); // [1, 5, 8]

Example

To copy just those elements from a range that satisfy a predicate, use filter:

import std.algorithm.iteration : filter;
int[] src = [ 1, 5, 8, 9, 10, 1, 2, 0 ];
auto dest = new int[src.length];
auto rem = src
    .filter!(a => (a & 1) == 1)
    .copy(dest);
writeln(dest[0 .. $ - rem.length]); // [1, 5, 9, 1]

Example

std.range.retro can be used to achieve behavior similar to STL's copy_backward':

import std.algorithm, std.range;
int[] src = [1, 2, 4];
int[] dest = [0, 0, 0, 0, 0];
src.retro.copy(dest.retro);
writeln(dest); // [0, 0, 1, 2, 4]

Authors

Andrei Alexandrescu

License

Boost License 1.0.