[alg.shift]

# 26 Algorithms library [[algorithms]](./#algorithms)

## 26.7 Mutating sequence operations [[alg.modifying.operations]](alg.modifying.operations#alg.shift)

### 26.7.14 Shift [alg.shift]

[ð](#lib:shift_left)

`template<class ForwardIterator>
  constexpr ForwardIterator
    shift_left(ForwardIterator first, ForwardIterator last,
               typename iterator_traits<ForwardIterator>::difference_type n);
template<class ExecutionPolicy, class ForwardIterator>
  ForwardIterator
    shift_left(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last,
               typename iterator_traits<ForwardIterator>::difference_type n);

template<[permutable](alg.req.permutable#concept:permutable "24.3.7.6 Concept permutable [alg.req.permutable]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel_for "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]")<I> S>
  constexpr subrange<I> ranges::shift_left(I first, S last, iter_difference_t<I> n);
template<[forward_range](range.refinements#concept:forward_range "25.4.6 Other range refinements [range.refinements]") R>
  requires [permutable](alg.req.permutable#concept:permutable "24.3.7.6 Concept permutable [alg.req.permutable]")<iterator_t<R>>
  constexpr borrowed_subrange_t<R> ranges::shift_left(R&& r, range_difference_t<R> n);

template<[execution-policy](algorithms.parallel.defns#concept:execution-policy "26.3.1 Preamble [algorithms.parallel.defns]") Ep, [random_access_iterator](iterator.concept.random.access#concept:random_access_iterator "24.3.4.13 Concept random_­access_­iterator [iterator.concept.random.access]") I, [sized_sentinel_for](iterator.concept.sizedsentinel#concept:sized_sentinel_for "24.3.4.8 Concept sized_­sentinel_­for [iterator.concept.sizedsentinel]")<I> S>
  requires [permutable](alg.req.permutable#concept:permutable "24.3.7.6 Concept permutable [alg.req.permutable]")<I>
  subrange<I>
    ranges::shift_left(Ep&& exec, I first, S last, iter_difference_t<I> n);
template<[execution-policy](algorithms.parallel.defns#concept:execution-policy "26.3.1 Preamble [algorithms.parallel.defns]") Ep, [sized-random-access-range](range.refinements#concept:sized-random-access-range "25.4.6 Other range refinements [range.refinements]") R>
  requires [permutable](alg.req.permutable#concept:permutable "24.3.7.6 Concept permutable [alg.req.permutable]")<iterator_t<R>>
  borrowed_subrange_t<R>
    ranges::shift_left(Ep&& exec, R&& r, range_difference_t<R> n);
`

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L8554)

*Preconditions*: n >= 0 is true[.](#1.sentence-1)

For the overloads in namespace std,
the type of *first meets the [*Cpp17MoveAssignable*](utility.arg.requirements#:Cpp17MoveAssignable "16.4.4.2 Template argument requirements [utility.arg.requirements]") requirements[.](#1.sentence-2)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L8560)

*Effects*: If n == 0 or n >= last - first, does nothing[.](#2.sentence-1)

Otherwise, moves the element
from position first + n + i into position first + i for each non-negative integer i < (last - first) - n[.](#2.sentence-2)

For the non-parallel algorithm overloads,
does so in order starting
from i = 0 and proceeding to i = (last - first) - n - 1[.](#2.sentence-3)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L8571)

*Returns*: Let *NEW_LAST* be first + (last - first - n) if n < last - first,
otherwise first[.](#3.sentence-1)

Returns:

- [(3.1)](#3.1)

  *NEW_LAST* for the overloads in namespace std[.](#3.1.sentence-1)

- [(3.2)](#3.2)

  {first, *NEW_LAST*} for the overloads in namespace ranges[.](#3.2.sentence-1)

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L8585)

*Complexity*: At most (last - first) - n assignments[.](#4.sentence-1)

[ð](#lib:shift_right)

`template<class ForwardIterator>
  constexpr ForwardIterator
    shift_right(ForwardIterator first, ForwardIterator last,
                typename iterator_traits<ForwardIterator>::difference_type n);
template<class ExecutionPolicy, class ForwardIterator>
  ForwardIterator
    shift_right(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last,
                typename iterator_traits<ForwardIterator>::difference_type n);

template<[permutable](alg.req.permutable#concept:permutable "24.3.7.6 Concept permutable [alg.req.permutable]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel_for "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]")<I> S>
  constexpr subrange<I> ranges::shift_right(I first, S last, iter_difference_t<I> n);
template<[forward_range](range.refinements#concept:forward_range "25.4.6 Other range refinements [range.refinements]") R>
  requires [permutable](alg.req.permutable#concept:permutable "24.3.7.6 Concept permutable [alg.req.permutable]")<iterator_t<R>>
  constexpr borrowed_subrange_t<R> ranges::shift_right(R&& r, range_difference_t<R> n);

template<[execution-policy](algorithms.parallel.defns#concept:execution-policy "26.3.1 Preamble [algorithms.parallel.defns]") Ep, [random_access_iterator](iterator.concept.random.access#concept:random_access_iterator "24.3.4.13 Concept random_­access_­iterator [iterator.concept.random.access]") I, [sized_sentinel_for](iterator.concept.sizedsentinel#concept:sized_sentinel_for "24.3.4.8 Concept sized_­sentinel_­for [iterator.concept.sizedsentinel]")<I> S>
  requires [permutable](alg.req.permutable#concept:permutable "24.3.7.6 Concept permutable [alg.req.permutable]")<I>
  subrange<I>
    ranges::shift_right(Ep&& exec, I first, S last, iter_difference_t<I> n);
template<[execution-policy](algorithms.parallel.defns#concept:execution-policy "26.3.1 Preamble [algorithms.parallel.defns]") Ep, [sized-random-access-range](range.refinements#concept:sized-random-access-range "25.4.6 Other range refinements [range.refinements]") R>
  requires [permutable](alg.req.permutable#concept:permutable "24.3.7.6 Concept permutable [alg.req.permutable]")<iterator_t<R>>
  borrowed_subrange_t<R>
    ranges::shift_right(Ep&& exec, R&& r, range_difference_t<R> n);
`

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L8618)

*Preconditions*: n >= 0 is true[.](#5.sentence-1)

For the overloads in namespace std,
the type of *first meets the [*Cpp17MoveAssignable*](utility.arg.requirements#:Cpp17MoveAssignable "16.4.4.2 Template argument requirements [utility.arg.requirements]") requirements,
and ForwardIterator meets
the [*Cpp17BidirectionalIterator*](bidirectional.iterators#:Cpp17BidirectionalIterator "24.3.5.6 Bidirectional iterators [bidirectional.iterators]") requirements ([[bidirectional.iterators]](bidirectional.iterators "24.3.5.6 Bidirectional iterators")) or
the [*Cpp17ValueSwappable*](swappable.requirements#:Cpp17ValueSwappable "16.4.4.3 Swappable requirements [swappable.requirements]") requirements[.](#5.sentence-2)

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L8627)

*Effects*: If n == 0 or n >= last - first, does nothing[.](#6.sentence-1)

Otherwise, moves the element
from position first + i into position first + n + i for each non-negative integer i < (last - first) - n[.](#6.sentence-2)

Does so in order starting
from i = (last - first) - n - 1 and proceeding to i = 0 if

- [(6.1)](#6.1)

for the non-parallel algorithm overload in namespace std,ForwardIterator meets the *Cpp17BidirectionalIterator* requirements,

- [(6.2)](#6.2)

for the non-parallel algorithm overloads in namespace ranges,I models [bidirectional_iterator](iterator.concept.bidir#concept:bidirectional_iterator "24.3.4.12 Concept bidirectional_­iterator [iterator.concept.bidir]")[.](#6.sentence-3)

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L8644)

*Returns*: Let *NEW_FIRST* be first + n if n < last - first,
otherwise last[.](#7.sentence-1)

Returns:

- [(7.1)](#7.1)

  *NEW_FIRST* for the overloads in namespace std[.](#7.1.sentence-1)

- [(7.2)](#7.2)

  {*NEW_FIRST*, last} for the overloads in namespace ranges[.](#7.2.sentence-1)

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L8657)

*Complexity*: At most (last - first) - n assignments or swaps[.](#8.sentence-1)
