On this page
std::ranges::reverse_copy, std::ranges::reverse_copy_result
Defined in header <algorithm> |
||
---|---|---|
Call signature | ||
|
(1) | (since C++20) |
|
(2) | (since C++20) |
Helper types | ||
|
(3) | (since C++20) |
1) Copies the elements from the source range
[
first
,
last
)
to the destination range [
result
,
result + N
)
, where N
is ranges::distance(first, last)
, in such a way that the elements in the new range are in reverse order. Behaves as if by executing the assignment *(result + N - 1 - i) = *(first + i)
once for each integer i
in [
0
,
N
)
. The behavior is undefined if the source and destination ranges overlap.
2) Same as (1), but uses
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. |
Return value
{last, result + N}
.
Complexity
Exactly N
assignments.
Notes
Implementations (e.g. MSVC STL) may enable vectorization when the both iterator types model contiguous_iterator
and have the same value type, and the value type is TriviallyCopyable.
Possible implementation
See also the implementations in MSVC STL and libstdc++.
|
Example
#include <algorithm>
#include <iostream>
#include <string>
int main()
{
std::string x {"12345"}, y(x.size(), ' ');
std::cout << x << " → ";
std::ranges::reverse_copy(x.begin(), x.end(), y.begin());
std::cout << y << " → ";
std::ranges::reverse_copy(y, x.begin());
std::cout << x << '\n';
}
Output:
12345 → 54321 → 12345
See also
(C++20)
|
reverses the order of elements in a range (niebloid) |
creates a copy of a range that is reversed (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/algorithm/ranges/reverse_copy