Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iterator>
  5.  
  6. template <typename T>
  7. class SequenceGenerator {
  8. public:
  9.     SequenceGenerator(T istart = T(), T istep = 1) :
  10.         start(istart), step(istep) {}
  11.     T operator()() {
  12.         T temp = start;
  13.         start += step;
  14.         return temp;
  15.     }
  16. private:
  17.     T start; // the start value of the sequence
  18.     T step;  // the step
  19. };
  20.  
  21. using namespace std;
  22.  
  23. int main() {
  24.     vector<int> v(10); // allocate 10 elements
  25.    
  26.     /*
  27.      * Output: 10 15 20 25 30 35 40 45 50 55
  28.      */
  29.     generate(v.begin(), v.end(), SequenceGenerator<int>(10, 5));
  30.     copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
  31.     cout << endl;
  32.    
  33.     /*
  34.      * Output: 5 6 7 8 9 10 11 12 13 14
  35.      */
  36.     generate(v.begin(), v.end(), SequenceGenerator<int>(5));
  37.     copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
  38.     cout << endl;
  39.    
  40.     /*
  41.      * Output: 0 1 2 3 4 5 6 7 8 9
  42.      */
  43.     generate(v.begin(), v.end(), SequenceGenerator<int>());
  44.     copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
  45.     cout << endl;
  46.    
  47.     /*
  48.      * Output: abcd...xyz
  49.      */
  50.     vector<char> chars('z' - 'a' + 1);
  51.     generate(chars.begin(), chars.end(), SequenceGenerator<char>('a'));
  52.     copy(chars.begin(), chars.end(), ostream_iterator<char>(cout));
  53.     cout << endl;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement