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
beginand the number of positions
nas arguments and fills the first
nposition starting from the position pointed by
beginwith the given
value.
Syntax:
void fill_n(iterator begin, int n, type value);Parameters
:
: This function does not returns any value. Below program illustrate the fill_n() function in C++ STL:
CPP14
// C++ program to demonstrate working of fil_n()
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> vect(8);
// calling fill to initialize first four values
// to 7
fill_n(vect.begin(), 4, 7);
for (int i = 0; i < vect.size(); i++)
cout << ' ' << vect[i];
cout << '\n';
// calling fill to initialize 3 elements from
// "begin()+3" with value 4
fill_n(vect.begin() + 3, 3, 4);
for (int i = 0; i < vect.size(); i++)
cout << ' ' << vect[i];
cout << '\n';
return 0;
}
Output:
7 7 7 7 0 0 0 0 7 7 7 4 4 4 0 0Reference
:
https://cplusplus.com/reference/algorithm/fill_n/RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4