fill_n


Set n items to a particular value.

Assign value to the n elements starting at position first . Return an iterator equal to result + n .

Library

Standards<ToolKit>

Declaration


#include <algorithm>

template< class OutputIterator, class Size, class T >
void fill_n( OutputIterator first, Size n, const T& value );

Complexity

Time complexity is linear as exactly n assignments are performed. Space complexity is constant.

Example <ospace/osstd/examples/filln1.cpp>
#include <iostream>
#include <algorithm>
#include <vector>

void
main()
  {
  vector< int > v( 10 );

  fill_n( v.begin(), v.size(), 42 );

  for ( int i = 0; i < 10; ++i )
    cout << v[ i ] << ` `;
  cout << "\n";
  }

42 42 42 42 42 42 42 42 42 42

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.