Home C++ Algorithms Functions move_backward() C++ std::move_backward() Function 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.
std::move_backward(v.begin(), v.begin() + 3, v.end());
std::move_backward(src.begin(), src.end(), dst.end()); Call What 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 template<class BidirIt1, class BidirIt2>
BidirIt2 move_backward(BidirIt1 first, BidirIt1 last, BidirIt2 d_last); Parameters Parameter Description first, last Required. The source range. d_last Required. The end of the destination. The last element is moved just before it.
Return Value Return Value Description Iterator Points at the first element that was written in the destination.
C++ Examples 1. Shift numbers right 2. Move to the end of a vector 3. Move smart pointers Example 1 — Shift numbers right Move three numbers two places to the right.
#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 The source is the first three numbers. The destination ends at v.end(). Moving from the back means 3 goes first, then 2, then 1, so nothing is overwritten early. 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.
#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 dst starts with four empty strings.The strings land in the last three slots, so the first slot stays empty. 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.
#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 The vector has two pointers and one empty slot at the end. move_backward() moves each pointer one place right, starting with the second one.The front slot is now a moved-from unique_ptr, which is always null. Frequently Asked Questions What does std::move_backward() do? In C++, std::move_backward() moves a range into a destination, starting from the last element. Include <algorithm> to use it.
Which header and C++ version do I need for std::move_backward()? Include <algorithm>. It needs C++11 or newer, so compile with -std=c++11 or later.
What does std::move_backward() return? It returns an iterator to the first element moved in the destination.
How is move_backward() different from copy_backward()? Both work from the last element to the first. move_backward() moves the elements, so it also works for types that cannot be copied.
When do I need the backward version? Use it when the destination overlaps the source and starts after it, for example when shifting elements to the right.
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.
10 people found this page helpful
Helpful Share Copy link Suggestion