C++ std::shift_right() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++20

Definition

In C++20, std::shift_right() slides every element n places toward the end of the range. The last n elements fall off.

The first n positions are left in an unspecified state, so you usually fill them with new values.

Syntax

Add #include <algorithm>, then call std::shift_right(). Start with these two lines.

C++
auto it = std::shift_right(v.begin(), v.end(), 2);
std::fill(v.begin(), it, 0);
CallWhat it does
auto it = std::shift_right(v.begin(), v.end(), 2);Shift right by two and get the start of the shifted data.
std::fill(v.begin(), it, 0);Fill the gap at the front.

Compile with -std=c++20 or newer.

Library declarations
C++
template<class ForwardIt>
ForwardIt shift_right(ForwardIt first, ForwardIt last,
                      typename iterator_traits<ForwardIt>::difference_type n);

Parameters

ParameterDescription
first, lastRequired. The range to shift.
nRequired. How many places to move the elements. It must not be negative.

Return Value

Return ValueDescription
IteratorPoints at the new first element of the shifted data (first + n).
lastReturned when n is as large as the range. Nothing is changed.

C++ Examples

Example 1 — Shift and print

Print the part that was shifted.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5};

    auto it = std::shift_right(v.begin(), v.end(), 2);

    std::cout << "Moved part: ";
    for (auto it = it; it != v.end(); ++it) std::cout << *it << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. The original 1, 2 and 3 slide two places to the right.
  2. 4 and 5 fall off the end and are lost.
  3. The returned iterator marks where the moved data starts, so we print from there.

Example 2 — Fill the gap

Put zeros in the freed space.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5};

    auto it = std::shift_right(v.begin(), v.end(), 2);
    std::fill(v.begin(), it, 0);

    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. After the shift the first two slots hold unspecified values.
  2. std::fill(v.begin(), it, 0) covers exactly those two slots.
  3. The vector now looks like a right shift with zero padding.

Example 3 — Insert at the front

Make room for a new first element.

C++
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> v = {"b", "c", "d"};

    std::shift_right(v.begin(), v.end(), 1);
    v[0] = "a";

    for (const std::string& x : v) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. Each string moves one place to the right.
  2. The last string, d, falls off the end.
  3. Slot 0 is set by hand to a.

Frequently Asked Questions

In C++, std::shift_right() moves the elements of a range toward the back by n places. Include <algorithm> to use it.
Include <algorithm>. It needs C++20 or newer, so compile with -std=c++20 or later.
It returns an iterator to the first element of the shifted data.
No. The size stays the same. The last n elements are lost.
rotate() wraps elements around. shift_right() drops the ones that fall off the end.

Next: shuffle()

Randomly reorders the elements of a range using a random engine.

shuffle() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

21 people found this page helpful