In C++20, std::shift_left() slides every element n places toward the front of the range. The first n elements are dropped.
The elements at the end are left in an unspecified (moved-from) state. The function returns the new end of the shifted data, so you can call erase() on the leftover part.
Syntax
Add #include <algorithm>, then call std::shift_left(). Start with these two lines.
C++
auto it = std::shift_left(v.begin(), v.end(), 2);
v.erase(it, v.end());
Call
What it does
auto it = std::shift_left(v.begin(), v.end(), 2);
Shift left by two and get the new end.
v.erase(it, v.end());
Delete the unspecified tail.
Compile with -std=c++20 or newer.
Library declarations
C++
template<class ForwardIt>
ForwardIt shift_left(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 end of the shifted data (first + (size - n)).
first
Returned when n is as large as the range. Nothing is changed.
C++ Examples
Example 1 — Drop the first two
Shift left and erase the leftover tail.
C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
auto it = std::shift_left(v.begin(), v.end(), 2);
v.erase(it, v.end());
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
Output
3 4 5
How It Works
The elements 3, 4 and 5 slide two places to the front.
1 and 2 are overwritten, so they are gone.
erase() removes the three leftover slots at the end.
Example 2 — Shift by too much
See what happens when n is not smaller than the range.
C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3};
auto it = std::shift_left(v.begin(), v.end(), 5);
std::cout << std::boolalpha << "Result is empty: " << (it == v.begin()) << '\n';
std::cout << "Vector: ";
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
Output
Result is empty: true
Vector: 1 2 3
How It Works
The range has only three elements, so a shift of five leaves nothing.
The function returns v.begin() and does not change the vector.
That is why the vector still prints 1 2 3.
Example 3 — Remove the first character
Cut one letter from the front of a string.
C++
#include <algorithm>
#include <iostream>
#include <string>
int main() {
std::string s = "Hello";
auto it = std::shift_left(s.begin(), s.end(), 1);
s.erase(it, s.end());
std::cout << s << '\n';
return 0;
}
Output
ello
How It Works
Every character moves one place to the left.
The H is overwritten by e.
Erasing from the returned iterator cuts the last, now duplicated, character.
Frequently Asked Questions
In C++, std::shift_left() moves the elements of a range toward the front 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 new end of the shifted elements.
C++20. Use -std=c++20 or newer.
They are left in an unspecified state. Erase them or give them new values.