C++ std::copy_backward() Function

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

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.

C++
std::copy_backward(v.begin(), v.begin() + 5, v.end());
std::copy_backward(src.begin(), src.end(), dst.end());
CallWhat 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
C++
template<class BidirIt1, class BidirIt2>
BidirIt2 copy_backward(BidirIt1 first, BidirIt1 last, BidirIt2 d_last);

Parameters

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

Return Value

Return ValueDescription
IteratorPoints 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.

C++
#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

  1. The source is the first five numbers. The destination ends at v.end().
  2. Copying backward moves 5 first, then 4, 3, 2 and 1, so no value is overwritten too soon.
  3. 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.

C++
#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

  1. dst.end() marks where the copy must finish.
  2. The last source value, 3, goes into the last slot, then 2, then 1.
  3. 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.

C++
#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

  1. resize(5) adds one extra slot at the end of the vector.
  2. copy_backward() slides 40 and 50 one place to the right to fill it.
  3. 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.

Next: copy_if()

Copies only the elements that satisfy a condition.

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

19 people found this page helpful