C++ std::copy_n() Function

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.
std::copy_n(src.begin(), 3, dst.begin());
std::copy_n(src.begin(), 2, std::back_inserter(dst));
| Call | What 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
template<class InputIt, class Size, class OutputIt>
OutputIt copy_n(InputIt first, Size count, OutputIt result);
Parameters
| Parameter | Description |
|---|
| first | Required. The first source element. |
| count | Required. How many elements to copy. A value of zero or less copies nothing. |
| result | Required. Where the copy starts in the destination. |
Return Value
| Return Value | Description |
|---|
| Iterator | Points 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.
#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
- The count 3 limits the copy, so
src.end() is not needed. dst has three slots, which matches the count.- The values 1, 2 and 3 land in the destination.
Example 2 — Take two strings
Append the first two words of a list.
#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
std::back_inserter makes some grow with each copied word.- Only two elements are taken, so
blue is left behind. - The copied words keep the order of the source.
Example 3 — Read from a stream
Take only the first two numbers from text.
#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
std::istringstream turns a string into an input stream.std::istream_iterator<int> reads numbers from it one at a time.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.
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.
24 people found this page helpful