CKTIS / C++
std::ranges::copy — copying range elements
std::ranges::copy copies elements in input order to a destination specified by an output iterator. It accepts either a whole range or an iterator and sentinel, and returns the final input and output positions.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <algorithm>
std::ranges::copy(first, last, result);
std::ranges::copy(range, result);std::ranges::copy, declared in <algorithm>, assigns successive input elements to the output. Overloads without an execution policy accept a range or an iterator–sentinel pair; the sentinel need not have the iterator's type. Input must satisfy input_range or input_iterator, and output must satisfy weakly_incrementable and the corresponding indirectly_copyable requirement.
The output must support writing every element. The algorithm receives no output bound and does not itself grow a container: an ordinary iterator requires existing destination elements. The output start must not lie in [first, last); overlapping leftward copying is allowed when this condition holds. It performs exactly N assignments for N elements.
The std::ranges::copy_result result exposes .in and .out: the final input iterator and the iterator past the last written element. For a temporary range that does not satisfy borrowed_range, .in has type std::ranges::dangling. Unlike std::copy, the result is not just an output iterator.
The example uses separate arrays and the range overload. This entry targets C++23 usage; the linked standard draft is updated and may contain later extensions.
Examples
#include <algorithm>
#include <array>
#include <iostream>
int main() {
const std::array<int, 3> source{2, 4, 6};
std::array<int, 3> destination{};
const auto result = std::ranges::copy(source, destination.begin());
std::cout << destination[0] << ' ' << destination[1] << ' '
<< destination[2] << '\n';
std::cout << (result.in == source.end()) << ' '
<< (result.out == destination.end()) << '\n';
}
Result
2 4 6 1 1
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.