C++ std::array fill() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::array

Definition

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('*');
CallWhat 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

ParameterDescription
valueRequired. The value to copy into every element. It must be the same type as the array elements, or convertible to it.

Return Value

Return ValueDescription
voidNothing 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;
}

How It Works

  1. nums is created without values, so its contents are random until we set them.
  2. nums.fill(7) copies 7 into all five slots.
  3. 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;
}

How It Works

  1. The first loop shows the original values 1 2 3 4.
  2. fill(0) replaces every old value with 0 at once, with no loop of your own.
  3. 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;
}

How It Works

  1. The array type is char, so the value to fill must be a character like '*'.
  2. Every one of the five slots now holds a star.
  3. 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.

Next: front()

Get the first element of the array with std::array.

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

8 people found this page helpful