std::rotate(3)
πΈ NAME πΈ
Hey bestie π this is std::rotate(3), straight outta the C++ Standard Libary (yes, that's the spelling in the header β we don't fix, we appreciate π«Ά). The name section is literally std::rotate - std::rotate, because why be subtle when you can be iconic?
β¨ Synopsis β¨
Defined in header <algorithm> π¦
template< class ForwardIt >
ForwardIt rotate( ForwardIt first, ForwardIt middle, (1)
(constexpr since C++20)
ForwardIt last );
template< class ExecutionPolicy, class ForwardIt >
ForwardIt rotate( ExecutionPolicy&& policy, (2) (since
C++17)
ForwardIt first, ForwardIt middle,
ForwardIt last );
- Performs a left rotation on a range of elements. Specifically,
std::rotateswaps the elements in the range[first, last)in such a way that the elements in[first, middle)are placed after the elements in[middle, last)while the orders of the elements in both ranges are preserved. Order preservation is the whole vibe π - 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)
π¨ Undefined behavior alert, bestie π¨ If any of the following conditions is satisfied, the behavior is undefined:
[first, middle)or[middle, last)is not a valid range.- The type of
*firstis notSwappable. (until C++11) ForwardItis notValueSwappable.- The type of
*firstis notMoveConstructible. (since C++11) - The type of
*firstis notMoveAssignable.
π Parameters π
firstβ the beginning of the original rangemiddleβ the element that should appear at the beginning of the rotated rangelastβ the end of the original rangepolicyβ the execution policy to use. See execution policy for details.
π¦ Type requirements π¦
ForwardItmust meet the requirements ofLegacyForwardIterator.
π Return value π
The iterator to the element originally referenced by *first, i.e. the std::distance(middle, last)th next iterator of first.
π Complexity π
At most std::distance(first, last) swaps.
π 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>
constexpr // since C++20
ForwardIt rotate(ForwardIt first, ForwardIt middle, ForwardIt last)
{
if (first == middle)
return last;
if (middle == last)
return first;
ForwardIt write = first;
ForwardIt next_read = first; // read position for when βreadβ hits βlastβ
for (ForwardIt read = middle; read != last; ++write, ++read)
{
if (write == next_read)
next_read = read; // track where βfirstβ went
std::iter_swap(write, read);
}
// rotate the remaining sequence into place
rotate(write, next_read, last);
return write;
}
β Notes β
std::rotate has better efficiency on common implementations if ForwardIt satisfies LegacyBidirectionalIterator or (better) LegacyRandomAccessIterator π₯
Implementations (e.g. MSVC STL) may enable vectorization when the iterator type satisfies LegacyContiguousIterator and swapping its value type calls neither non-trivial special member function nor ADL-found swap.
π Example π
std::rotate is a common building block in many algorithms. This example demonstrates insertion sort.
The original even says // Run this code before the example, so here it is π»
#include <algorithm>
#include <iostream>
#include <vector>
auto print = [](const auto remark, const auto& v)
{
std::cout << remark;
for (auto n : v)
std::cout << n << ' ';
std::cout << '\n';
};
int main()
{
std::vector<int> v{2, 4, 2, 0, 5, 10, 7, 3, 7, 1};
print("before sort:\t\t", v);
// insertion sort
for (auto i = v.begin(); i != v.end(); ++i)
std::rotate(std::upper_bound(v.begin(), i, *i), i, i + 1);
print("after sort:\t\t", v);
// simple rotation to the left
std::rotate(v.begin(), v.begin() + 1, v.end());
print("simple rotate left:\t", v);
// simple rotation to the right
std::rotate(v.rbegin(), v.rbegin() + 1, v.rend());
print("simple rotate right:\t", v);
}
π Output π
before sort: 2 4 2 0 5 10 7 3 7 1
after sort: 0 1 2 2 3 4 5 7 7 10
simple rotate left: 1 2 2 3 4 5 7 7 10 0
simple rotate right: 0 1 2 2 3 4 5 7 7 10
π₯ Defect reports π₯
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR
LWG 488Β· applied to C++98 β behavior as published: the new location of the element pointed byfirstwas not returned; correct behavior:returned.
π See also π
rotate_copyβ copies and rotate a range of elements (function template)ranges::rotateβ rotates the order of elements in a range (C++20) (niebloid)
Source/version/date check, bestie: http://cppreference.com, 2024.06.10, std::rotate(3) β mwah πβ¨
π¦ 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