Replaces n elements with a given value
You're viewing an older version of this page (#4141). View the current version.
View versions (1)
Definition
The fill_n() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.
Interface
#include <algorithm>
template < class OutputIterator, class Size, class Type >
void fill_n(
OutputIterator first,
Size count,
const Type& val
);
Parameters:
| Parameter | Description |
| first | An output iterator addressing the position of the first element in the range to be assigned the value val |
| count | A signed or unsigned integer type specifying the number of elements to be assigned the value |
| val | The value to be assigned to elements in the range [first, first + count) |
Description
Fill_n algorithm does the same thing that http://www.codecogs.com/reference/computing/stl/algorithms/modifying/fill.php"fill" but fills n elements.
Return Value
None.
Complexity
The complexity is linear; performs n assignments.
References
Example 1
ProblemThe following program demonstrates how to use fill_n() 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 3 positions for 6 position with a value of 9
cout <<"\nOperation: fill_n(vec.begin() + 3, 6, 9)\n";
fill_n(vec.begin() + 3, 6, 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_n(vec.begin() + 3, 6, 9)
Modified vec data: 10 11 12 9 9 9 9 9 9 19 20
Example 2
ProblemThis program illustrates the use of the STL fill_n() algorithm to set a given number values starting at a given location in 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 5 values of v, starting at the 3rd, to 50.";
fill_n(v.begin()+2, 5, 50);
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 the last three values of v to -1.";
fill_n(v.begin()+7, 3, -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 100.";
fill_n(v.begin(), v.size(), 100);
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 5 values of v, starting at the 3rd, to 50.
Here are the revised contents of v:
1 2 50 50 50 50 50 8 9 10
Now we set the last three values of v to -1.
Here are the revised contents of v:
1 2 50 50 50 50 50 -1 -1 -1
Finally, we initialize the entire vector to 100.
Here are the revised contents of v:
100 100 100 100 100 100 100 100 100 100
See Also