std::fill(start, end, value);
// Example 1: Fill a vector with a value #include#include #include int main() { std::vector myvector(5); // create a vector with 5 elements std::fill(myvector.begin(), myvector.end(), 10); // fill the vector with 10s // print the vector for (auto &i : myvector) { std::cout << i << '\n'; } return 0; } // Output: // 10 // 10 // 10 // 10 // 10
// Example 2: Fill an array with a value #includeIn both examples, std::fill is used to fill a range (vector or array) with a value (10 in the first example or 0 in the second example). The std::fill function is part of the C++ Standard Library (STL) and can be found in the#include int main() { int myarray[5]; // create an array with 5 elements std::fill(myarray, myarray+5, 0); // fill the array with 0s // print the array for (auto &i : myarray) { std::cout << i << '\n'; } return 0; } // Output: // 0 // 0 // 0 // 0 // 0