# 11 Algorithms library [algorithms]

## 11.4 Mutating sequence operations [alg.modifying.operations]

### 11.4.11 Rotate [alg.rotate]

``` template <ForwardIterator I, Sentinel<I> S> requires Permutable<I> tagged_pair<tag::begin(I), tag::end(I)> rotate(I first, I middle, S last); template <ForwardRange Rng> requires Permutable<iterator_t<Rng>> tagged_pair<tag::begin(safe_iterator_t<Rng>), tag::end(safe_iterator_t<Rng>)> rotate(Rng&& rng, iterator_t<Rng> middle); ```

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).

Returns: {first + (last - middle), last}.

Remarks: This is a left rotate.

Requires: [first,middle) and [middle,last) shall be valid ranges.

Complexity: At most last - first swaps.

``` template <ForwardIterator I, Sentinel<I> S, WeaklyIncrementable O> requires IndirectlyCopyable<I, O> tagged_pair<tag::in(I), tag::out(O)> rotate_copy(I first, I middle, S last, O result); template <ForwardRange Rng, WeaklyIncrementable O> requires IndirectlyCopyable<iterator_t<Rng>, O> tagged_pair<tag::in(safe_iterator_t<Rng>), tag::out(O)> rotate_copy(Rng&& rng, iterator_t<Rng> middle, O result); ```

Effects: Copies the range [first,last) to the range [result,result + (last - first)) such that for each non-negative integer i < (last - first) the following assignment takes place: *(result + i) = *(first + (i + (middle - first)) % (last - first)).

Returns: {last, result + (last - first)}.

Requires: The ranges [first,last) and [result,result + (last - first)) shall not overlap.

Complexity: Exactly last - first assignments.