On this page
std::ranges::rotate_copy, std::ranges::rotate_copy_result
Defined in header <algorithm> |
||
---|---|---|
Call signature | ||
|
(1) | (since C++20) |
|
(2) | (since C++20) |
Helper types | ||
|
(3) | (since C++20) |
[
first
,
last
)
, to the destination range beginning at result
in such a way, that the element *middle
becomes the first element of the destination range and *(middle - 1)
becomes the last element. The result is that the destination range contains a left rotated copy of the source range.
[
first
,
middle
)
or [
middle
,
last
)
is not a valid range, or the source and 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 source range of elements to copy from |
r | - | the source range of elements to copy from |
middle | - | the iterator to the element that should appear at the beginning of the destination range |
result | - | beginning of the destination range |
Return value
{last, result + N}
, where N = ranges::distance(first, last)
.
Complexity
Linear: exactly N
assignments.
Notes
If the value type is TriviallyCopyable and the iterator types satisfy contiguous_iterator
, implementations of ranges::rotate_copy
usually avoid multiple assignments by using a "bulk copy" function such as std::memmove
.
Possible implementation
See also the implementations in libstdc++ and MSVC STL.
|
Example
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
std::vector<int> src {1, 2, 3, 4, 5};
std::vector<int> dest(src.size());
auto pivot = std::ranges::find(src, 3);
std::ranges::rotate_copy(src, pivot, dest.begin());
for (int i : dest)
std::cout << i << ' ';
std::cout << '\n';
// copy the rotation result directly to the std::cout
pivot = std::ranges::find(dest, 1);
std::ranges::rotate_copy(dest, pivot, std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
}
Output:
3 4 5 1 2
1 2 3 4 5
See also
(C++20)
|
rotates the order of elements in a range (niebloid) |
(C++20)(C++20)
|
copies a range of elements to a new location (niebloid) |
copies and rotate a range of elements (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/algorithm/ranges/rotate_copy