C++ std::shift_left() Function

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

Definition

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());
CallWhat 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

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 end of the shifted data (first + (size - n)).
firstReturned 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;
}

How It Works

  1. The elements 3, 4 and 5 slide two places to the front.
  2. 1 and 2 are overwritten, so they are gone.
  3. 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;
}

How It Works

  1. The range has only three elements, so a shift of five leaves nothing.
  2. The function returns v.begin() and does not change the vector.
  3. 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;
}

How It Works

  1. Every character moves one place to the left.
  2. The H is overwritten by e.
  3. 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.

Next: shift_right()

Moves the elements of a range toward the back by n places.

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

12 people found this page helpful