Using fill_n value to fill value to vector - C++ STL Algorithm

C++ examples for STL Algorithm:fill_n

Description

Using fill_n value to fill value to vector

Demo Code

#include <iostream> 
#include <algorithm> // algorithm definitions 
#include <vector> // vector class-template definition 
#include <iterator> // ostream_iterator 
using namespace std; 

int main() //from   w  w  w  . jav a2s . com
{ 
   vector< char > chars( 10 ); 
   ostream_iterator< char > output( cout, " " ); 
   fill( chars.begin(), chars.end(), '5' ); // fill chars with 5s 

   cout << "Vector chars after filling with 5s:\n"; 
   copy( chars.begin(), chars.end(), output ); 

   // fill first five elements of chars with As 
   fill_n( chars.begin(), 5, 'A' ); 

   cout << "\n\nVector chars after filling five elements with As:\n"; 
   copy( chars.begin(), chars.end(), output ); 


}

Result


Related Tutorials