C++ std::fill_n() Function

Definition
In C++, std::fill_n() writes the same value into n elements, starting at an iterator.
Because it takes a count, it works well with std::back_inserter to add n copies of a value to a vector.
Syntax
Add #include <algorithm>, then call std::fill_n(). Start with these two lines.
std::fill_n(v.begin(), 3, 9);
std::fill_n(std::back_inserter(v), 4, 1);
| Call | What it does |
|---|
std::fill_n(v.begin(), 3, 9); | Set the first three elements to 9. |
std::fill_n(std::back_inserter(v), 4, 1); | Append four copies of 1. |
Library declarations
template<class OutputIt, class Size, class T>
OutputIt fill_n(OutputIt first, Size count, const T& value);
Parameters
| Parameter | Description |
|---|
| first | Required. Where to start writing. |
| count | Required. How many elements to write. |
| value | Required. The value to write. |
Return Value
| Return Value | Description |
|---|
| Iterator | Points just after the last element written. |
C++ Examples
Example 1 — Fill the first three
Overwrite the start of a vector.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v(6, 0);
std::fill_n(v.begin(), 3, 9);
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
- The vector starts with six zeros.
- The count 3 limits the writes to the first three slots.
- The last three zeros are not touched.
Example 2 — Grow with back_inserter
Create four copies of a value in an empty vector.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main() {
std::vector<int> v;
std::fill_n(std::back_inserter(v), 4, 1);
std::cout << "Size: " << v.size() << '\n';
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
- The vector starts empty, so a normal iterator would have nowhere to write.
std::back_inserter calls push_back() for each of the four writes.- The vector ends with size 4.
Example 3 — Fill part of an array
Use a plain array and a pointer.
#include <algorithm>
#include <iostream>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
std::fill_n(arr + 1, 3, 0);
for (int x : arr) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
- An array name works like a pointer, and pointers are iterators.
arr + 1 skips the first element, then three elements are set to 0.- The first and last elements keep their values.
Frequently Asked Questions
In C++, std::fill_n() assigns a value to the first n elements of a range. 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 element after the last one that was written.
fill() needs two iterators. fill_n() needs a start iterator and a count.
Only with std::back_inserter. With a normal iterator it just overwrites existing elements.
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