C++ std::shift_right() Function

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.
auto it = std::shift_right(v.begin(), v.end(), 2);
std::fill(v.begin(), it, 0);
| Call | What 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
template<class ForwardIt>
ForwardIt shift_right(ForwardIt first, ForwardIt last,
typename iterator_traits<ForwardIt>::difference_type n);
Parameters
| Parameter | Description |
|---|
| first, last | Required. The range to shift. |
| n | Required. How many places to move the elements. It must not be negative. |
Return Value
| Return Value | Description |
|---|
| Iterator | Points at the new first element of the shifted data (first + n). |
last | Returned when n is as large as the range. Nothing is changed. |
C++ Examples
Example 1 — Shift and print
Print the part that was shifted.
#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
- The original 1, 2 and 3 slide two places to the right.
- 4 and 5 fall off the end and are lost.
- 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.
#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
- After the shift the first two slots hold unspecified values.
std::fill(v.begin(), it, 0) covers exactly those two slots.- The vector now looks like a right shift with zero padding.
Example 3 — Insert at the front
Make room for a new first element.
#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
- Each string moves one place to the right.
- The last string,
d, falls off the end. - 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.
About the author
Developer, cloud engineer, and technical writer
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