C++ Array fill() Function

In this blog post, we will learn how to use the C++ Array fill() Function With Some straightforward explanations with easy examples.

C++ Array fill() function

This is a public member function.

The C++ function std::array::fill() sets given value to all elements of array means that it just Fill array with value.


val − value to be set

Sets Val as the value for all the elements in the array object.

Time complexity

Linear:  Size of the array object i.e. O(n)


// C++ array::fill example
#include <iostream>
#include <array>

int main () {
  std::array<int,6> arr;


  std::cout << "arr contains:";
  for ( int& x : arr) { std::cout << ' ' << x; }

  std::cout << '\n';

  return 0;


arr contains: 3 3 3 3 3 3


In the above code, we have an array name ‘arr’ which have a size of 6 and after using the arr.fill(3) function we are filling the array named ‘arr’ with 3 number in all index of the array here length of the array is 6 that’s why it is printing 3 3 3 3 3 3. so using arr. fill() function we can able to fill array element by giving parameter to arr. fill() function.

I hope that you are now able to understand how to use the array fill() function in C++.

Leave a Reply

Your email address will not be published.