C++ std::fill() Function

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

Definition

In C++, std::fill() sets every element in a range to one value. It is a quick way to reset a vector or fill a string.

To create a vector already filled with a value, you can also write std::vector<int> v(5, 7);. Use fill() when the container already exists.

Syntax

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

C++
std::fill(v.begin(), v.end(), 7);
std::fill(v.begin() + 2, v.begin() + 4, 0);
CallWhat it does
std::fill(v.begin(), v.end(), 7);Set every element to 7.
std::fill(v.begin() + 2, v.begin() + 4, 0);Fill only part of the vector.
Library declarations
C++
template<class ForwardIt, class T>
void fill(ForwardIt first, ForwardIt last, const T& value);

Parameters

ParameterDescription
first, lastRequired. The range to fill.
valueRequired. The value to write into each element.

Return Value

Return ValueDescription
voidNothing is returned.

C++ Examples

Example 1 — Fill a whole vector

Set every element of a vector to 7.

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

int main() {
    std::vector<int> v(5);

    std::fill(v.begin(), v.end(), 7);

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

How It Works

  1. v(5) creates five elements, each set to 0 at first.
  2. fill() visits all five and writes 7 into each.
  3. The vector keeps its size of 5.

Example 2 — Fill part of a vector

Clear two elements in the middle.

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

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

    std::fill(v.begin() + 2, v.begin() + 4, 0);

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

How It Works

  1. v.begin() + 2 starts at the third element and v.begin() + 4 stops before the fifth.
  2. Only the elements at index 2 and 3 are overwritten with 0.
  3. The rest of the vector is left alone.

Example 3 — Hide a secret

Replace every character of a string with a star.

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

int main() {
    std::string secret = "secret";

    std::fill(secret.begin(), secret.end(), '*');

    std::cout << secret << '\n';
    return 0;
}

How It Works

  1. A string is a range of characters, so fill() works on it.
  2. Each of the six letters is overwritten with '*'.
  3. The length stays the same, so you get six stars.

Frequently Asked Questions

In C++, std::fill() assigns the same value to every element in 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 nothing (void). Every element in the range is set to the value.
No. It only changes the values of elements that already exist. Use resize() or the constructor to change the size.
fill() takes a start and an end. fill_n() takes a start and a count.

Next: fill_n()

Assigns a value to the first n elements of a range.

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

12 people found this page helpful