In C++, std::array::fill() sets every element of the array to the same value. If the array has N elements, all N of them are changed.
It is a quick way to reset an array to zero or to start with a known value. The size of the array does not change. Include <array> to use it.
This is a member of std::array<T, N>, not of a plain C array and not of std::vector. A std::array has a fixed size N that is part of its type, and it lives on the stack like a normal variable.
Syntax
Call fill with the value you want in every slot.
C++
nums.fill(7);
letters.fill('*');
Call
What it does
nums.fill(7)
Makes every element equal to 7.
nums.fill(0)
Resets the whole array to zero.
If you only want to change one element, use at() or [] instead.
Library declarations
C++
void fill(const T& value);
Parameters
Parameter
Description
value
Required. The value to copy into every element. It must be the same type as the array elements, or convertible to it.
Return Value
Return Value
Description
void
Nothing is returned. The array itself is changed.
C++ Examples
Example 1 — Basic fill()
Fill a new array with one number.
C++
#include <array>
#include <iostream>
int main() {
std::array<int, 5> nums;
nums.fill(7);
for (int n : nums) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
7 7 7 7 7
How It Works
nums is created without values, so its contents are random until we set them.
nums.fill(7) copies 7 into all five slots.
The loop prints each slot, so you see five sevens.
Example 2 — Reset an Array
Print the array before and after clearing it with fill(0).
C++
#include <array>
#include <iostream>
int main() {
std::array<int, 4> nums = {1, 2, 3, 4};
std::cout << "Before: ";
for (int n : nums) std::cout << n << ' ';
std::cout << '\n';
nums.fill(0);
std::cout << "After: ";
for (int n : nums) std::cout << n << ' ';
std::cout << '\n';
return 0;
}
Output
Before: 1 2 3 4
After: 0 0 0 0
How It Works
The first loop shows the original values 1 2 3 4.
fill(0) replaces every old value with 0 at once, with no loop of your own.
The second loop proves it, and the size is still 4.
Example 3 — Fill with a Character
fill() works with any element type, such as char.
C++
#include <array>
#include <iostream>
int main() {
std::array<char, 5> line;
line.fill('*');
for (char c : line) {
std::cout << c;
}
std::cout << '\n';
return 0;
}
Output
*****
How It Works
The array type is char, so the value to fill must be a character like '*'.
Every one of the five slots now holds a star.
The loop prints the characters side by side with no spaces, which draws a line of stars.
Frequently Asked Questions
It sets all elements of the array to the value you give. For an array of N elements, all N are changed.
No. A std::array always keeps its size N. fill() only changes the values.
fill() always covers the whole array. For a part of it, use std::fill with two iterators, for example std::fill(nums.begin(), nums.begin() + 2, 0).
The same type as the array elements, or something that converts to it. For std::array<int, 5>, pass an int.
Both can set all elements to zero. std::array<int, 5> a{}; does it when the array is created, and a.fill(0); does it later whenever you need to reset the array.