![]() |
VOOZH | about |
std::fill is a C++ STL algorithm defined in the <algorithm> header that assigns a specified value to every element in a given range [first, last). It works with arrays and STL containers that provide at least forward iterators (such as vector, list, and deque).
0 0 4 4 4 4 4 0
Explanation: This code fills the elements from index 2 to 6 of the vector with the value 4 using std::fill.
std::fill(first, last, value);
Note: The range is half-open — first is included, but last is excluded.
Example 1: Using std::fill with an Array
4 4 4 4 4 4 4 4 4 4
Explanation: This code assigns the value 4 to all elements of the array by applying std::fill on the entire array range.
Example 2: Using std::fill with a List
4 4 4
Explanation: This code updates every element of the list to 4 by calling std::fill from begin() to end()