24 Algorithms library [algorithms]

24.6 Mutating sequence operations [alg.modifying.operations]

24.6.11 Rotate [alg.rotate]

template<class ForwardIterator> constexpr ForwardIterator rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last); template<class ExecutionPolicy, class ForwardIterator> ForwardIterator rotate(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator middle, ForwardIterator last); namespace ranges { template<Permutable I, Sentinel<I> S> constexpr subrange<I> rotate(I first, I middle, S last); }
Requires: [first, middle) and [middle, last) shall be valid ranges.
For the overloads in namespace std, ForwardIterator shall meet the Cpp17ValueSwappable requirements ([swappable.requirements]), and the type of *first shall meet the Cpp17MoveConstructible (Table 26) and Cpp17MoveAssignable (Table 28) requirements.
Effects: For each non-negative integer i < (last - first), places the element from the position first + i into position first + (i + (last - middle)) % (last - first).
[Note
:
This is a left rotate.
end note
]
Returns:
  • first + (last - middle) for the overloads in namespace std, or
  • {first + (last - middle), last} for the overload in namespace ranges.
Complexity: At most last - first swaps.
namespace ranges { template<ForwardRange R> requires Permutable<iterator_t<R>> constexpr safe_subrange_t<R> rotate(R&& r, iterator_t<R> middle); }
Effects: Equivalent to: return ranges::rotate(ranges::begin(r), middle, ranges::end(r));
template<class ForwardIterator, class OutputIterator> constexpr OutputIterator rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2> ForwardIterator2 rotate_copy(ExecutionPolicy&& exec, ForwardIterator1 first, ForwardIterator1 middle, ForwardIterator1 last, ForwardIterator2 result); namespace ranges { template<ForwardIterator I, Sentinel<I> S, WeaklyIncrementable O> requires IndirectlyCopyable<I, O> constexpr rotate_copy_result<I, O> rotate_copy(I first, I middle, S last, O result); }
Let N be last - first.
Requires: [first, middle) and [middle, last) shall be valid ranges.
The ranges [first, last) and [result, result + N) shall not overlap.
Effects: Copies the range [first, last) to the range [result, result + N) such that for each non-negative integer the following assignment takes place: *(result + i) = *(first + (i + (middle - first)) % N).
Returns:
  • result + N for the overloads in namespace std, or
  • {last, result + N} for the overload in namespace ranges.
Complexity: Exactly N assignments.
namespace ranges { template<ForwardRange R, WeaklyIncrementable O> requires IndirectlyCopyable<iterator_t<R>, O> constexpr rotate_copy_result<safe_iterator_t<R>, O> rotate_copy(R&& r, iterator_t<R> middle, O result); }
Effects: Equivalent to:
return ranges::rotate_copy(ranges::begin(r), middle, ranges::end(r), result);