Replaces each element with a given value
View versions (1)
Definition
The fill() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.
Interface
#include <algorithm>
template < class ForwardIterator, class Type >
void fill(
ForwardIterator first,
ForwardIterator last,
const Type& val
);
Parameters:
| Parameter | Description |
| first | A forward iterator addressing the position of the first element in the range to be traversed |
| last | A forward iterator addressing the position one past the final element in the range to be traversed |
| val | The value to be assigned to elements in the range [first, last) |
Description
Fill algorithm fills a range with a given value by using operator=.
Return Value
None.
Complexity
The complexity is linear; performs (last - first) assignments.
References
Example 1
ProblemThe following program demonstrates how to use fill() function.
Workings#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec;
vector <int>::iterator Iter1;
int i;
for (i = 10; i <= 20; i++)
vec.push_back(i);
cout <<"Vector vec data: ";
for(Iter1 = vec.begin(); Iter1 != vec.end(); Iter1++)
cout <<*Iter1<<" ";
cout <<endl;
// fill the last 4 positions with a value of 9
cout <<"\nOperation: fill(vec.begin() + 4, vec.end(), 9)\n";
fill(vec.begin() + 4, vec.end(), 9);
cout <<"Modified vec data: ";
for (Iter1 = vec.begin(); Iter1 != vec.end(); Iter1++)
cout <<*Iter1<<" ";
cout <<endl;
return 0;
}
SolutionOutput:
Vector vec data: 10 11 12 13 14 15 16 17 18 19 20
Operation: fill(vec.begin() + 4, vec.end(), 9)
Modified vec data: 10 11 12 13 9 9 9 9 9 9 9
Example 2
ProblemThis program illustrates the use of the STL fill() algorithm to set all values in a range of values within a vector of integers to a given value.
Workings#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
vector<int> v(a, a+10);
cout <<"\nHere are the contents of v:\n";
for (vector<int>::size_type i=0; i<v.size(); i++)
cout <<v.at(i)<<" ";
cout <<"\nNow we set the 3rd to the 7th values of v to 20.";
fill(v.begin()+2, v.begin()+7, 20);
cout <<"\nHere are the revised contents of v:\n";
for (vector<int>::size_type i=0; i<v.size(); i++)
cout <<v.at(i)<<" ";
cout <<"\nNow we set all values from the 6th on to -1.";
fill(v.begin()+5, v.end(), -1);
cout <<"\nHere are the revised contents of v:\n";
for (vector<int>::size_type i=0; i<v.size(); i++)
cout <<v.at(i)<<" ";
cout <<"\nFinally, we initialize the entire vector to 0.";
fill(v.begin(), v.end(), 0);
cout <<"\nHere are the revised contents of v:\n";
for (vector<int>::size_type i=0; i<v.size(); i++)
cout <<v.at(i)<<" ";
return 0;
}
SolutionOutput:
Here are the contents of v:
1 2 3 4 5 6 7 8 9 10
Now we set the 3rd to the 7th values of v to 20.
Here are the revised contents of v:
1 2 20 20 20 20 20 8 9 10
Now we set all values from the 6th on to -1.
Here are the revised contents of v:
1 2 20 20 20 -1 -1 -1 -1 -1
Finally, we initialize the entire vector to 0.
Here are the revised contents of v:
0 0 0 0 0 0 0 0 0 0
See Also