Last Updated : 12 Jul, 2025
The fill() function in C++ STL is used to fill some default value in a container. The fill() function can also be used to fill values in a range in the container. It accepts two iterators
beginand
endand fills a value in the container starting from position pointed by
beginand just before the position pointed by
end.
Syntax:
void fill(iterator begin, iterator end, type value);Parameters
:
: Notice carefully that ‘begin’ is included in the range but ‘end’ is NOT included.
Return Value: This function does not returns any value. Below program illustrate the fill() function in C++ STL:
CPP14
// C++ program to demonstrate working of fill()
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> vect(8);
// calling fill to initialize values in the
// range to 4
fill(vect.begin() + 2, vect.end() - 1, 4);
for (int i = 0; i < vect.size(); i++)
cout << vect[i] << " ";
// Filling the complete vector with value 10
fill(vect.begin(), vect.end(), 10);
cout << endl;
for (int i = 0; i < vect.size(); i++)
cout << vect[i] << " ";
return 0;
}
Output:
0 0 4 4 4 4 4 0 10 10 10 10 10 10 10 10Reference
:
https://cplusplus.com/reference/algorithm/fill/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