Updated March 29, 2023
Introduction to C++ Fill()
C++ fill() function is a function present in the algorithm header file that helps to assign a specific value to the specified position of the given container, such as vector or arrays. The iterators pointing to the starting and end position where value needs to be assigned, and the value is passed as parameters to the function. This method is present in the algorithm header file of the std library. Also, this method can be used for any datatype; thus, its definition includes a Template definition. Here we must note that the end iterator is exclusive, but the start iterator in inclusive of the range.
Syntax
Fill function is one of the great utilities provided in C++ libraries in algorithm header files for filling the vector positions at one go. This method is very useful as one need not iterate to every position in the vector one by one.
This method is present in the algorithm header file; thus, it must be included in one’s program for using the fill function in one application.
For including the algorithm header file, we can use the same syntax to include the iostream header file.
#include<algorithm>
using namespace std;
Since we are the name of the namespace no need to add .h in the suffix of the header file name.
Std::fill(Iterator start, iterator end, value)
Here, the first 2 parameters are of iterator type that points to the beginning and end position of the vector. Next, we must specify the position from which the specific value must be filled in the vector.
For e.g., If one wants to fill a specific value at positions 2nd to the second last position in a vector, then we can specify vector.begin()+1 in the start iterator and vector.end()-1 in the end positions. Here we must note that the end position is not included in the range.
The next parameter is where we specify the value that needs to be filled in the vector using the fill function.
Example:
fill(vector.begin()+2, vector.end()-1,5)
How to fill() algorithm function work in C++?
Fill function is a great utility provided by the algorithm header file in the std library of C++. This function takes 3 parameters as follows:-
- Starting position – This parameter is an iterator type that specifies the starting position of the container where the value needs to be filled.
- End position – This parameter is also of iterator type that specifies the end +1 position in the container where the specific value needs to be filled. We must note that this position is exclusive; that is, this position is not included when the function’s value is assigned.
- Value – This parameter is the value that needs to be assigned to the specific positions of the container. The data type of the value must be the same as the data type of the container being declared.
Fill function reads the above values in the parameters and iterates through the container. If the position lies in the given range, the value specified in the third parameter is assigned to that position.
Examples of C++ Fill()
Different examples are mentioned below:
Example #1
In this first example, we will see how to use the fill function to fill positions from 2nd to third last positions of a given vector named as my_list of length 8 with the specified value 6. And then, we will again use the fill function to fill value 2 at positions starting from 5 till ending position.
Code:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<int> my_list(8);
fill(my_list.begin() + 1, my_list.end() - 2, 6);
cout<<"Filling 6 at positions 2 to 6 : ";
for (int i=0; i<my_list.size(); i++)
cout << my_list[i] << " ";
cout<<endl;
cout<<"Filling 2 at positions 5 to 8 : ";
fill(my_list.begin() + 4, my_list.end(), 2);
for (int i=0; i<my_list.size(); i++)
cout << my_list[i] << " ";
return 0;
Output
Example #2
In this example, we will perform a fill function to assign values into an array. Here we will also in case value is not assigned to a particular position in an array, garbage value gets reflected.
Code:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int my_arr[12];
fill(my_arr, my_arr + 8, 7);
cout<<"Values in array after filling first 8 positions : ";
for (int i = 0; i < 12; i++)
cout << my_arr[i] << " ";
cout<<endl;
fill(my_arr+8, my_arr + 11, 9);
cout<<"Values in array after filling positions from 9 to 12 : ";
for (int i = 0; i < 12; i++)
cout << my_arr[i] << " ";
return 0;
}
Output
Example #3
In this example, we will see to assign the values in the list type container. Then, we will initiate the list with some values but replace those values using the fill function.
Code:
#include <iostream>
#include <bits/stdc++.h>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
list<int> my_list = {7, 17, 27,34,67,8,44,9 };
cout<<"Values in my list before filling 10 at all positions : ";
for (int x : my_list)
cout << x << " ";
cout<<endl;
cout<<"Values in my list after filling 10 at all positions : ";
fill(my_list.begin(), my_list.end(), 10);
for (int x : my_list)
cout << x << " ";
cout<<endl;
return 0;
}
Output
Conclusion
Fill function is a utility present in the algorithm header file of the std library that helps to assign a specific value to particular positions in the container. It accepts the iterators pointing to starting and ending index in the container with the value and fills the value at those positions.
Recommended Articles
This is a guide to C++ Fill(). Here we discuss How to fill() algorithm function work in C++ and Examples along with codes and outputs. You may also have a look at the following articles to learn more –