fill() – fill array

In this program, an std::array object called myArray is declared and initialized with the values 1 through 9. The std::array container is defined by specifying the element type and the size of the array in angle brackets. In this case, the element type is int and the size is determined by the number of initializers provided.

The program then uses the fill() member function of the std::array container to fill all elements of the myArray array with the value 1. This function takes a single argument, which is the value to fill the array with.

The program then uses a for loop to iterate over the elements of the myArray array and print each value to the console using the cout statement.

The using namespace std; statement at the beginning of the program allows the program to use names from the std namespace without explicitly qualifying them.

#include <iostream>
#include <array>
using namespace std;

int main() {
    array<int, 9> myArray = {1, 2, 3, 4, 5, 6, 7, 8, 9};

    myArray.fill(1);

    for (int i = 0; i < 9; i++)
        cout << myArray[i] << " ";

    return 0;
}
Output
1 1 1 1 1 1 1 1 1