C++ std::move_backward() Function

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

Definition

In C++, std::move_backward() is the moving version of std::copy_backward(). You pass the end of the destination, and the elements are moved from last to first.

Use it to shift elements to the right inside one container, especially when the elements are costly to copy.

Syntax

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

C++
std::move_backward(v.begin(), v.begin() + 3, v.end());
std::move_backward(src.begin(), src.end(), dst.end());
CallWhat it does
std::move_backward(v.begin(), v.begin() + 3, v.end());Move the first three elements so they end at the end of the vector.
std::move_backward(src.begin(), src.end(), dst.end());Move a whole source to the end of a destination.
Library declarations
C++
template<class BidirIt1, class BidirIt2>
BidirIt2 move_backward(BidirIt1 first, BidirIt1 last, BidirIt2 d_last);

Parameters

ParameterDescription
first, lastRequired. The source range.
d_lastRequired. The end of the destination. The last element is moved just before it.

Return Value

Return ValueDescription
IteratorPoints at the first element that was written in the destination.

C++ Examples

Example 1 — Shift numbers right

Move three numbers two places to the right.

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

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

    std::move_backward(v.begin(), v.begin() + 3, v.end());

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

How It Works

  1. The source is the first three numbers. The destination ends at v.end().
  2. Moving from the back means 3 goes first, then 2, then 1, so nothing is overwritten early.
  3. For int a move is just a copy, which is why 1 and 2 still show at the front.

Example 2 — Move to the end of a vector

Place three strings at the end of a bigger vector.

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

int main() {
    std::vector<std::string> src = {"x", "y", "z"};
    std::vector<std::string> dst(4);

    std::move_backward(src.begin(), src.end(), dst.end());

    for (const std::string& s : dst) {
        std::cout << '[' << s << ']';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. dst starts with four empty strings.
  2. The strings land in the last three slots, so the first slot stays empty.
  3. Square brackets make the empty first string visible in the output.

Example 3 — Move smart pointers

Make a gap at the front of a vector of unique_ptr.

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

int main() {
    std::vector<std::unique_ptr<int>> v(3);
    v[0] = std::make_unique<int>(1);
    v[1] = std::make_unique<int>(2);

    std::move_backward(v.begin(), v.begin() + 2, v.end());

    std::cout << *v[1] << ' ' << *v[2] << '\n';
    std::cout << std::boolalpha << "Front is empty: " << (v[0] == nullptr) << '\n';
    return 0;
}

How It Works

  1. The vector has two pointers and one empty slot at the end.
  2. move_backward() moves each pointer one place right, starting with the second one.
  3. The front slot is now a moved-from unique_ptr, which is always null.

Frequently Asked Questions

In C++, std::move_backward() moves a range into a destination, starting from the last element. Include <algorithm> to use it.
Include <algorithm>. It needs C++11 or newer, so compile with -std=c++11 or later.
It returns an iterator to the first element moved in the destination.
Both work from the last element to the first. move_backward() moves the elements, so it also works for types that cannot be copied.
Use it when the destination overlaps the source and starts after it, for example when shifting elements to the right.

Next: next_permutation()

Rearranges a range into the next permutation in dictionary order.

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

10 people found this page helpful