C++ std::copy_n() Function

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

Definition

In C++, std::copy_n() copies exactly n elements from a starting iterator. You do not need an end iterator, just a count.

It is useful when you know how many values you want, for example the first three items or the next two numbers from a stream.

Syntax

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

C++
std::copy_n(src.begin(), 3, dst.begin());
std::copy_n(src.begin(), 2, std::back_inserter(dst));
CallWhat it does
std::copy_n(src.begin(), 3, dst.begin());Copy the first three elements.
std::copy_n(src.begin(), 2, std::back_inserter(dst));Append two elements to a growing vector.
Library declarations
C++
template<class InputIt, class Size, class OutputIt>
OutputIt copy_n(InputIt first, Size count, OutputIt result);

Parameters

ParameterDescription
firstRequired. The first source element.
countRequired. How many elements to copy. A value of zero or less copies nothing.
resultRequired. Where the copy starts in the destination.

Return Value

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

C++ Examples

Example 1 — Copy the first three

Copy part of a vector into a smaller one.

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

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

    std::copy_n(src.begin(), 3, dst.begin());

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

How It Works

  1. The count 3 limits the copy, so src.end() is not needed.
  2. dst has three slots, which matches the count.
  3. The values 1, 2 and 3 land in the destination.

Example 2 — Take two strings

Append the first two words of a list.

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

int main() {
    std::vector<std::string> all = {"red", "green", "blue"};
    std::vector<std::string> some;

    std::copy_n(all.begin(), 2, std::back_inserter(some));

    for (const std::string& x : some) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. std::back_inserter makes some grow with each copied word.
  2. Only two elements are taken, so blue is left behind.
  3. The copied words keep the order of the source.

Example 3 — Read from a stream

Take only the first two numbers from text.

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

int main() {
    std::istringstream in("10 20 30 40");
    std::vector<int> v;

    std::copy_n(std::istream_iterator<int>(in), 2, std::back_inserter(v));

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

How It Works

  1. std::istringstream turns a string into an input stream.
  2. std::istream_iterator<int> reads numbers from it one at a time.
  3. copy_n() stops after two numbers, so 30 and 40 are never read.

Frequently Asked Questions

In C++, std::copy_n() copies a fixed number of elements to another place. 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 position after the last copied element in the destination.
copy() takes two source iterators. copy_n() takes one iterator and a count.
Nothing is copied, and the function returns the destination iterator unchanged.

Next: count()

Counts how many elements equal a given value.

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

24 people found this page helpful