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.
Parameters
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)
Example:
// C++ array::fill example #include <iostream> #include <array> int main () { std::array<int,6> arr; arr.fill(3); std::cout << "arr contains:"; for ( int& x : arr) { std::cout << ' ' << x; } std::cout << '\n'; return 0; }
Output:
arr contains: 3 3 3 3 3 3
Explanation:
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