VOOZH about

URL: https://www.geeksforgeeks.org/cpp/fill-in-cpp-stl/

⇱ fill in C++ STL - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

fill in C++ STL

Last Updated : 20 Jan, 2026

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).


Output
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.

Syntax

std::fill(first, last, value);

Parameters

  • first: Iterator pointing to the first element of the range.
  • last: Iterator pointing to the position just after the last element of the range.
  • value: The value to be assigned to each element in the range.

Note: The range is half-open — first is included, but last is excluded.

Example 1: Using std::fill with an Array 


Output
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


Output
4 4 4 

Explanation: This code updates every element of the list to 4 by calling std::fill from begin() to end()

Comment
Article Tags: