Generate() return values returned by a generator function. - C++ STL Algorithm

C++ examples for STL Algorithm:generate

Description

Generate() return values returned by a generator function.

Demo Code

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
double pow_of_two();
int main()/*from w w  w  . j  a  v a2s.  c om*/
{
   vector<double> v(5);
   // Generate a sequence.
   generate(v.begin(), v.end(), pow_of_two);
   cout << "Powers of 2: ";
   for(unsigned i=0; i < v.size(); ++i)
      cout << v[i] << " ";
   return 0;
}
// A simple generator function that generates the powers of 2.
double pow_of_two() {
   static double val = 1.0;
   double t;
   t = val;
   val += val;
   return t;
}

Result


Related Tutorials