fill_n() function in C++ STL with examples
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:
// 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; } |
7 7 7 7 0 0 0 0 7 7 7 4 4 4 0 0
Reference: http://www.cplusplus.com/reference/algorithm/fill_n/
Recommended Posts:
- ios eof() function in C++ with Examples
- ios bad() function in C++ with Examples
- ios fail() function in C++ with Examples
- wcscpy() function in C++ with Examples
- norm() function in C++ with Examples
- wcslen() function in C++ with Examples
- fill() function in C++ STL with examples
- wcsspn() function in C/C++ with Examples
- wcscmp() function in C++ with Examples
- ios operator !() function in C++ with Examples
- btowc() function in C/C++ with Examples
- ios good() function in C++ with Examples
- ios clear() function in C++ with Examples
- ios operator() function in C++11 with Examples
- ios rdstate() 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.



