fill_n() function in C++ STL with examples Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: 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 0 Reference: https://cplusplus.com/reference/algorithm/fill_n/ Comment More infoAdvertise with us Next Article set emplace_hint() function in C++ STL H harsh.agarwal0 Follow Improve Article Tags : Misc C++ CPP-Functions Practice Tags : CPPMisc Similar Reads 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 pos 2 min read fill() and fill_n() functions in C++ STL A vector, once declared, has all its values initialized to zero. Following is an example code to demonstrate the same. CPP // C++ program for displaying the default initialization // of the vector vect[] #include<bits/stdc++.h> using namespace std; int main() { // Creating a vector of size 8 v 3 min read set emplace_hint() function in C++ STL The set::emplace_hint() is a built-in function in C++ STL which inserts a new element in the set. A position is passed in the parameter of the function which acts as a hint from where the searching operation starts before inserting the element at its current position. The position only helps the pro 2 min read copy_n() Function in C++ STL Copy_n() is the C++ function defined in <algorithm> library in STL. It helps to copy one array element to the new array. Copy_n function allows the freedom to choose how many elements must be copied in the destination container. This function takes 3 arguments, the source array name, the size 2 min read map erase() Function in C++ STL In C++, std::map::erase() is a built-in function of std::map container that is used to remove elements from the map using their key or iterator. We can also remove multiple elements using iterators. In this article, we will learn how to use the map::erase() in C++SyntaxThe std::string::erase() funct 3 min read Map of Tuples in C++ with Examples What is a tuple? A tuple in C++ is an object that has the ability to group a number of elements. The elements can be of the same type as well as different data types. The order in which tuple elements are initialized can be accessed in the same order. Functions associated with a tuple: 1. make_tuple 4 min read Like