fill() function in C++ STL with examples
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 begin and end and fills a value in the container starting from position pointed by begin and just before the position pointed by end.
Syntax:
void fill(iterator begin, iterator end, type value);
Parameters:
- begin: The function will start filling values from the position pointed by the iterator begin.
- end: The function will fill values upto the position just before the position pointed by the iterator end.
- value: This parameter denotes the value to be filled by the function in the container.
NOTE : 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:
// 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; } |
0 0 4 4 4 4 4 0 10 10 10 10 10 10 10 10
Reference: http://www.cplusplus.com/reference/algorithm/fill/
Recommended Posts:
- fill in C++ STL
- C++ Program that will fill whole memory
- fill() and fill_n() functions in C++ STL
- ios eof() function in C++ with Examples
- ios bad() function in C++ with Examples
- ios operator() function in C++ with Examples
- wcslen() function in C++ with Examples
- wcscpy() function in C++ with Examples
- fill_n() function in C++ STL with examples
- btowc() function in C/C++ with Examples
- wcscmp() function in C++ with Examples
- norm() function in C++ with Examples
- ios good() function in C++ with Examples
- ios setstate() function in C++ with Examples
- ios fail() function in C++ with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



