C++ std::copy_backward() Function

Definition
In C++, std::copy_backward() copies elements starting from the last one. You give it the end of the destination, and it fills backward.
This order is important when the destination overlaps the source on the right side, such as when you shift elements to make space.
Syntax
Add #include <algorithm>, then call std::copy_backward(). Start with these two lines.
std::copy_backward(v.begin(), v.begin() + 5, v.end());
std::copy_backward(src.begin(), src.end(), dst.end());
| Call | What it does |
|---|
std::copy_backward(v.begin(), v.begin() + 5, v.end()); | Copy the first five elements so they end at the end of the vector. |
std::copy_backward(src.begin(), src.end(), dst.end()); | Copy a source so it fills the end of the destination. |
Library declarations
template<class BidirIt1, class BidirIt2>
BidirIt2 copy_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 source element is written just before it. |
Return Value
| Return Value | Description |
|---|
| Iterator | Points at the first element that was written in the destination. |
C++ Examples
Example 1 — Shift right with overlap
Move five numbers two places to the right inside one vector.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5, 0, 0};
std::copy_backward(v.begin(), v.begin() + 5, v.end());
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
- The source is the first five numbers. The destination ends at
v.end(). - Copying backward moves 5 first, then 4, 3, 2 and 1, so no value is overwritten too soon.
- The old front values 1 and 2 stay where they were, which is why they appear twice.
Example 2 — Fill the end of a vector
Place three numbers at the end of a longer vector.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> src = {1, 2, 3};
std::vector<int> dst(5, 0);
std::copy_backward(src.begin(), src.end(), dst.end());
for (int x : dst) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
dst.end() marks where the copy must finish.- The last source value, 3, goes into the last slot, then 2, then 1.
- The first two slots keep their original 0 values.
Example 3 — Make room for an insert
Open a gap, then place a new number in it.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {10, 20, 40, 50};
v.resize(5);
std::copy_backward(v.begin() + 2, v.begin() + 4, v.end());
v[2] = 30;
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
resize(5) adds one extra slot at the end of the vector.copy_backward() slides 40 and 50 one place to the right to fill it.- The gap at index 2 is then filled with 30 by hand.
Frequently Asked Questions
In C++, std::copy_backward() copies a range into a destination, working from the last element to the first. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns an iterator to the first element copied in the destination.
Use it when the destination overlaps the source and starts to the right of it. Copying forward would overwrite values before they are copied.
No. It copies in reverse order, but each element keeps its relative position.
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.
19 people found this page helpful