C++ std::fill_n() Function

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

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.

C++
std::fill_n(v.begin(), 3, 9);
std::fill_n(std::back_inserter(v), 4, 1);
CallWhat 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
C++
template<class OutputIt, class Size, class T>
OutputIt fill_n(OutputIt first, Size count, const T& value);

Parameters

ParameterDescription
firstRequired. Where to start writing.
countRequired. How many elements to write.
valueRequired. The value to write.

Return Value

Return ValueDescription
IteratorPoints just after the last element written.

C++ Examples

Example 1 — Fill the first three

Overwrite the start of a vector.

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

  1. The vector starts with six zeros.
  2. The count 3 limits the writes to the first three slots.
  3. The last three zeros are not touched.

Example 2 — Grow with back_inserter

Create four copies of a value in an empty vector.

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

  1. The vector starts empty, so a normal iterator would have nowhere to write.
  2. std::back_inserter calls push_back() for each of the four writes.
  3. The vector ends with size 4.

Example 3 — Fill part of an array

Use a plain array and a pointer.

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

  1. An array name works like a pointer, and pointers are iterators.
  2. arr + 1 skips the first element, then three elements are set to 0.
  3. 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.

Next: find()

Finds the first element in a range that equals a value.

find() 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