std::rotate (3) - Linux Man Pages
Defined in header <algorithm>
template< class ForwardIt > (until C++11)
void rotate( ForwardIt first, ForwardIt n_first, ForwardIt last );
template< class ForwardIt > (since C++11)
ForwardIt rotate( ForwardIt first, ForwardIt n_first, ForwardIt last ); (until C++20)
template< class ForwardIt > (1) (since C++20)
constexpr ForwardIt rotate( ForwardIt first, ForwardIt n_first, ForwardIt last );
template< class ExecutionPolicy, class ForwardIt >
ForwardIt rotate( ExecutionPolicy&& policy, (2) (since C++17)
ForwardIt first, ForwardIt n_first, ForwardIt last );
1) Performs a left rotation on a range of elements.
Specifically, std::rotate swaps the elements in the range [first, last) 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.
A precondition of this function is that [first, n_first) and [n_first, last) are valid ranges.
2) Same as (1), but executed according to policy. This overload does not participate in overload resolution unless std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true
first - the beginning of the original range
n_first - the element that should appear at the beginning of the rotated range
last - the end of the original range
policy - the execution policy to use. See execution_policy for details.
ForwardIt must meet the requirements of ValueSwappable and LegacyForwardIterator.
The type of dereferenced ForwardIt must meet the requirements of MoveAssignable and MoveConstructible.
(none) (until C++11)
The iterator equal to first + (last - n_first) (since C++11)
Linear in the distance between first and last
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 ExecutionPolicy is one of the standard_policies, std::terminate is called. For any other ExecutionPolicy, the behavior is implementation-defined.
* If the algorithm fails to allocate memory, std::bad_alloc is thrown.
See also the implementations in libstdc++ and libc++.
std::rotate is a common building block in many algorithms. This example demonstrates insertion sort:
// Run this code
rotate_copy (function template)