Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Exemple d’utilisation de l’agorithme ‘generate()’

 calcul, assert
{filelink=15348}

 
#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
 
template <typename T>
class calcul_puissance {
  T i;
 public:
  calcul_puissance(): i(0) {}
  T operator()() { ++i; return pow(i,2); }
};
 
int main()
{
  vector<int> vec(9);
 
  //Remplir les 9 éléments par l'appel successif de 'gen()'
  generate(vec.begin(), vec.end(), calcul_puissance<int>());
 
  for (int j = 0; j < 9; ++j)
    cout << vec[j] << " ";
  cout<<endl;
 
  return 0;
}
 
/*
1 4 9 16 25 36 49 64 81
 */

No comments yet.

Leave a Comment