Provide predicate for std::generate : generate « STL Algorithms Modifying sequence operations « C++






Provide predicate for std::generate

  
 

#include <iostream>
using std::cout;
using std::endl;

#include <algorithm>
#include <vector>
#include <iterator>

char nextLetter();

int main()
{
   std::vector< char > chars( 10 );
   std::ostream_iterator< char > output( cout, " " );

    // generate values for all elements of chars with nextLetter
   std::generate( chars.begin(), chars.end(), nextLetter );

   cout << "\n\nVector chars after generating letters A-J:\n";
   std::copy( chars.begin(), chars.end(), output );

   cout << endl;
   return 0;
}

// generator function returns next letter (starts with A)
char nextLetter() {
   static char letter = 'A';
   return letter++;
}

/* 


Vector chars after generating letters A-J:
A B C D E F G H I J

 */        
    
  








Related examples in the same category

1.Illustrating the generic generate algorithm: Fill vector1 with 1, 4, 9, 16, ..., 100
2.Use std::generate to fill elements in a vector
3.Int sequence
4.Use generate to insert five random numbers into a list
5.Generate a sequence.