C++ std::fill() Function

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.
std::fill(v.begin(), v.end(), 7);
std::fill(v.begin() + 2, v.begin() + 4, 0);
| Call | What 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
template<class ForwardIt, class T>
void fill(ForwardIt first, ForwardIt last, const T& value);
Parameters
| Parameter | Description |
|---|
| first, last | Required. The range to fill. |
| value | Required. The value to write into each element. |
Return Value
| Return Value | Description |
|---|
void | Nothing is returned. |
C++ Examples
Example 1 — Fill a whole vector
Set every element of a vector to 7.
#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
v(5) creates five elements, each set to 0 at first.fill() visits all five and writes 7 into each.- The vector keeps its size of 5.
Example 2 — Fill part of a vector
Clear two elements in the middle.
#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
v.begin() + 2 starts at the third element and v.begin() + 4 stops before the fifth.- Only the elements at index 2 and 3 are overwritten with 0.
- The rest of the vector is left alone.
Example 3 — Hide a secret
Replace every character of a string with a star.
#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
- A string is a range of characters, so
fill() works on it. - Each of the six letters is overwritten with
'*'. - 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.
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.
12 people found this page helpful