gen_fib.cpp
Upload User: dq031136
Upload Date: 2022-08-08
Package Size: 802k
Code Size: 2k
Development Platform:

C++ Builder

  1. //  If you use Visual C++, set the compile options to /GX
  2. #ifdef __BCPLUSPLUS__
  3. #include <iostream.h>
  4. #include <vector.h>
  5. #include <algorith.h>
  6. #else
  7. #include <iostream>
  8. #include <vector>
  9. #include <algorithm>
  10. #endif
  11. using namespace std;
  12. //return the next Fibonacci number in the Fibonacci series.
  13. int Fibonacci(void)
  14. {
  15. static int r;
  16. static int f1 = 0;
  17. static int f2 = 1;
  18. r = f1 + f2;
  19. f1 = f2;
  20. f2 = r;
  21. return f1;
  22. }
  23. void main(void)
  24. {
  25. const int VECTOR_SIZE = 15;
  26. //Define a template class vector of integers 
  27. typedef vector<int> IntVector;
  28. //Define a iterator for template class vector of integer
  29. typedef IntVector::iterator IntVectorIt;
  30. IntVector Numbers(VECTOR_SIZE); //vector containing numbers
  31. IntVectorIt start, end , it;
  32. int i;
  33. //Initialize vector Numbers
  34. for(i = 0; i < VECTOR_SIZE; i++)
  35. Numbers[i] = i * i;
  36. start = Numbers.begin(); //location of first element of Numbers
  37. end = Numbers.end(); //one past the location of the last elememt of Numbers
  38. cout << "Before calling generate_n" << endl;
  39. //print content of Numbers
  40. cout << "Numbers { ";
  41. for (it = start; it != end; it++)
  42. cout << *it << " ";
  43. cout << " }n" << endl;
  44. //fill the specified range with a series of 
  45. //Fibonacci numbers using the Fibonacci function
  46. generate_n(start + 5, Numbers.size() - 5, Fibonacci);
  47. cout << "After calling generate_n" << endl;
  48. //print content of Numbers
  49. cout << "Numbers {";
  50. for (it = start; it != end; it++)
  51. cout << *it << " ";
  52. cout << " }n" << endl;
  53. }