std::rotate_copy(3)
π
std::rotate_copy(3) π
okay so bestie, welcome to the <pink>std::rotate_copy</pink> page from the C++ Standard Libary manual β¨ (yes, thatβs the manual name, typo and all β we donβt fix, we report π
). This one is lowkey giving βcopy my range but start from this one spotβ energy, and honestly? iconic ππ.
πΈ NAME πΈ
std::rotate_copy - std::rotate_copy β that's it, that's the NAME section, and it ate π
β¨.
β¨ Synopsis β¨
Defined in header <algorithm>:
template< class ForwardIt, class OutputIt >
OutputIt rotate_copy( ForwardIt first, ForwardIt n_first,
ForwardIt last, OutputIt d_first );
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
ForwardIt2 rotate_copy( ExecutionPolicy&& policy, ForwardIt1 first,
ForwardIt1 n_first, ForwardIt1 last, ForwardIt2 d_first );
(1)βconstexpr since C++20β(2)βsince C++17π¦
so overload (1) copies the elements from the range [first, last) to another range beginning at d_first, in such a way that the element *(n_first) becomes the first element of the new range and *(n_first - 1) becomes the last element. no cap, thatβs the whole rotation-copy moment β¨.
overload (2) is same as (1), but executed according to policy. This overload participates in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true. (until C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true. (since C++20) π«Ά
π¨ vibe check: undefined behavior alert π¨ if any of the following conditions is satisfied, the behavior is undefined: -[first, n_first)or[n_first, last)is not a valid range. - The source and destination ranges overlap.
π Parameters π
first, lastβ the range of elements to copy πn_firstβ an iterator to an element in[first, last)that should appear at the beginning of the new range. itβs the pivot, the main character, the βstart here bestieβ iterator πd_firstβ beginning of the destination range π―policyβ the execution policy to use. See execution policy for details. π§
π¦ Type requirements π¦
ForwardIt,ForwardIt1,ForwardIt2must meet the requirements ofLegacyForwardIterator.OutputItmust meet the requirements ofLegacyOutputIterator.
β Return value β
Output iterator to the element past the last element copied. thatβs the βhereβs where I stopped, bestieβ iterator π«Ά.
π₯ Complexity π₯
Exactly std::distance(first, last) assignments. no more, no less, periodt π
.
π Exceptions π
The overload with a template parameter named ExecutionPolicy reports errors as follows:
- If execution of a function invoked as part of the algorithm throws an exception and
ExecutionPolicyis one of the standard policies,std::terminateis called. For any otherExecutionPolicy, the behavior is implementation-defined. - If the algorithm fails to allocate memory,
std::bad_allocis thrown.
π§ Possible implementation π§
See also the implementations in libstdc++, libc++, and MSVC STL.
template<class ForwardIt, class OutputIt>
constexpr // since C++20
OutputIt rotate_copy(ForwardIt first, ForwardIt n_first,
ForwardIt last, OutputIt d_first)
{
d_first = std::copy(n_first, last, d_first);
return std::copy(first, n_first, d_first);
}
π Example π
// Run this code
#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::find(src.begin(), src.end(), 3);
std::rotate_copy(src.begin(), pivot, src.end(), dest.begin());
for (int i : dest)
std::cout << i << ' ';
std::cout << '\n';
// copy the rotation result directly to the std::cout
pivot = std::find(dest.begin(), dest.end(), 1);
std::rotate_copy(dest.begin(), pivot, dest.end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
}
π Output π
3 4 5 1 2
1 2 3 4 5
π See also π
rotateβ rotates the order of elements in a range (function template) πranges::rotate_copyβ copies and rotate a range of elements (C++20) (niebloid) π
okay thatβs the whole page, bestie π«Ά β source: http://cppreference.com, version/date: 2024.06.10, page: std::rotate_copy(3) β¨π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms