VOOZH about

URL: https://www.geeksforgeeks.org/cpp/fill_n-function-in-c-stl-with-examples/

⇱ fill_n() function in C++ STL with examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

fill_n() function in C++ STL with examples

Last Updated : 12 Jul, 2025
The fill_n() function in C++ STL is used to fill some default values in a container. The fill_n() function is used to fill values upto first n positions from a starting position. It accepts an iterator begin and the number of positions n as arguments and fills the first n position starting from the position pointed by begin with the given value. Syntax:
void fill_n(iterator begin, int n, type value);
Parameters:
  • begin: The function will start filling values from the position pointed by the iterator begin.
  • n: This parameter denotes the number of positions to be filled starting from the position pointed by first parameter begin.
  • value: This parameter denotes the value to be filled by the function in the container.
Return Value: This function does not returns any value. Below program illustrate the fill_n() function in C++ STL:
Output:
7 7 7 7 0 0 0 0
 7 7 7 4 4 4 0 0
Reference: https://cplusplus.com/reference/algorithm/fill_n/
Comment
Article Tags: