C++ std::copy() Function

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

Definition

In C++, std::copy() copies every element from a source range into a destination, starting at the destination iterator you give it.

The destination can be another vector, an array, or even a stream. The source stays unchanged. Make sure the destination has enough room, or use std::back_inserter so it grows by itself.

Syntax

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

C++
std::copy(src.begin(), src.end(), dst.begin());
std::copy(src.begin(), src.end(), std::back_inserter(dst));
CallWhat it does
std::copy(src.begin(), src.end(), dst.begin());Copy into a destination that already has enough elements.
std::copy(src.begin(), src.end(), std::back_inserter(dst));Append to a vector that grows as needed.
Library declarations
C++
template<class InputIt, class OutputIt>
OutputIt copy(InputIt first, InputIt last, OutputIt d_first);

Parameters

ParameterDescription
first, lastRequired. The source range to copy.
d_firstRequired. Where the copy starts. It must have room for every element.

Return Value

Return ValueDescription
IteratorPoints just after the last element written in the destination.

C++ Examples

Example 1 — Copy into a sized vector

Copy three numbers into a vector that is already the right size.

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

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

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

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

How It Works

  1. dst(3) creates a vector with three slots, so there is room for the copy.
  2. copy() reads each source value and writes it into the next slot of dst.
  3. The source is untouched, and dst now holds the same three numbers.

Example 2 — Grow with back_inserter

Let the destination grow by itself.

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

int main() {
    std::vector<int> src = {4, 5, 6};
    std::vector<int> dst;

    std::copy(src.begin(), src.end(), std::back_inserter(dst));

    std::cout << "Size: " << dst.size() << '\n';
    for (int x : dst) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. dst starts empty, so dst.begin() would not work here.
  2. std::back_inserter(dst) calls push_back() for each copied value.
  3. The vector ends up with size 3 and the copied values in order.

Example 3 — Print with an iterator

Copy straight into std::cout.

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

int main() {
    std::vector<int> v = {7, 8, 9};

    std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " | "));
    std::cout << '\n';
    return 0;
}

How It Works

  1. std::ostream_iterator<int> is an output iterator that writes to a stream.
  2. The second argument, " | ", is printed after each number.
  3. So copy() can print a whole container with one line.

Frequently Asked Questions

In C++, std::copy() copies the elements of one range into another place. 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 position just after the last copied element in the destination.
A copy constructor makes a whole new vector. std::copy() copies elements into a destination that already exists, and works with any container or array.
The destination must not start inside the source range. Use std::copy_backward() when the destination overlaps on the right.

Next: copy_backward()

Copies a range into a destination, working from the last element to the first.

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

9 people found this page helpful