I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com


Replaces n elements with a given value
+ View version details

Key Facts

Gyroscopic Couple: The rate of change of angular momentum (\tau) = I\omega\Omega (In the limit).
  • I = Moment of Inertia.
  • \omega = Angular velocity
  • \Omega = Angular velocity of precession.

Blaise Pascal (1623-1662) was a French mathematician, physicist, inventor, writer and Catholic philosopher.

Leonhard Euler (1707-1783) was a pioneering Swiss mathematician and physicist.


The fill_n() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.


#include <algorithm>
template < class OutputIterator, class Size, class Type >
   void fill_n(
      OutputIterator first, 
      Size count, 
      const Type& val

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)


Fill_n algorithm does the same thing that fill but fills n elements.

Return Value



The complexity is linear; performs n assignments.

Example - fill_n function
The following program demonstrates how to use fill_n() function.
#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++)
  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;

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