On this page
std::ranges::copy, std::ranges::copy_if, std::ranges::copy_result, std::ranges::copy_if_result
Defined in header <algorithm> |
||
---|---|---|
Call signature | ||
|
(1) | (since C++20) |
|
(2) | (since C++20) |
|
(3) | (since C++20) |
|
(4) | (since C++20) |
Helper types | ||
|
(5) | (since C++20) |
|
(6) | (since C++20) |
Copies the elements in the range, defined by [
first
,
last
)
, to another range beginning at result
.
[
first
,
last
)
starting from first
and proceeding to last - 1
. The behavior is undefined if result
is within the range [
first
,
last
)
. In this case, ranges::copy_backward
may be used instead.
pred
returns true
. The relative order of the elements that are copied is preserved. The behavior is undefined if the source and the destination ranges overlap.
r
as the source range, as if using ranges::begin(r)
as first
and ranges::end(r)
as last
.
The function-like entities described on this page are niebloids, that is:
- Explicit template argument lists cannot be specified when calling any of them.
- None of them are visible to argument-dependent lookup.
- When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.
In practice, they may be implemented as function objects, or with special compiler extensions.
Parameters
first, last | - | the range of elements to copy |
r | - | the range of elements to copy |
result | - | the beginning of the destination range. |
pred | - | predicate to apply to the projected elements |
proj | - | projection to apply to the elements |
Return value
A ranges::in_out_result
containing an input iterator equal to last
and an output iterator past the last element copied.
Complexity
last - first
assignments.
last - first
applications of the predicate and projection, between 0
and last - first
assignments (assignment for every element for which predicate returns true
, dependent on predicate and input data).
Notes
In practice, implementations of ranges::copy
avoid multiple assignments and use bulk copy functions such as std::memmove
if the value type is TriviallyCopyable and the iterator types satisfy contiguous_iterator
.
When copying overlapping ranges, ranges::copy
is appropriate when copying to the left (beginning of the destination range is outside the source range) while ranges::copy_backward
is appropriate when copying to the right (end of the destination range is outside the source range).
Possible implementation
copy |
---|
|
copy_if |
|
Example
The following code uses ranges::copy
to both copy the contents of one std::vector
to another and to display the resulting std::vector
:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
int main()
{
std::vector<int> source(10);
std::iota(source.begin(), source.end(), 0);
std::vector<int> destination;
std::ranges::copy(source.begin(), source.end(),
std::back_inserter(destination));
// or, alternatively,
// std::vector<int> destination(source.size());
// std::ranges::copy(source.begin(), source.end(), destination.begin());
// either way is equivalent to
// std::vector<int> destination = source;
std::cout << "destination contains: ";
std::ranges::copy(destination, std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
std::cout << "odd numbers in destination are: ";
std::ranges::copy_if(destination, std::ostream_iterator<int>(std::cout, " "),
[](int x) { return (x % 2) == 1; });
std::cout << '\n';
}
Output:
destination contains: 0 1 2 3 4 5 6 7 8 9
odd numbers in destination are: 1 3 5 7 9
See also
(C++20)
|
copies a range of elements in backwards order (niebloid) |
(C++20)
|
creates a copy of a range that is reversed (niebloid) |
(C++20)
|
copies a number of elements to a new location (niebloid) |
(C++20)
|
assigns a range of elements a certain value (niebloid) |
(C++20)(C++20)
|
copies a range of elements omitting those that satisfy specific criteria (niebloid) |
(C++11)
|
copies a range of elements to a new location (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/algorithm/ranges/copy