funadap.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // funadap.cpp -- using function adapters
  2. #include <iostream>
  3. #include <vector>
  4. #include <iterator>
  5. #include <algorithm>
  6. #include <functional>
  7. void Show(double);
  8. const int LIM = 6;
  9. int main()
  10. {
  11. using namespace std;
  12. double arr1[LIM] = {28, 29, 30, 35, 38, 59};
  13. double arr2[LIM] = {63, 65, 69, 75, 80, 99};
  14. vector<double> gr8(arr1, arr1 + LIM);
  15. vector<double> m8(arr2, arr2 + LIM);
  16. cout.setf(ios_base::fixed);
  17. cout.precision(1);
  18. cout << "gr8:\t";
  19. for_each(gr8.begin(), gr8.end(), Show);
  20. cout << endl;
  21. cout << "m8: \t";
  22. for_each(m8.begin(), m8.end(), Show);
  23. cout << endl;
  24. vector<double> sum(LIM);
  25. transform(gr8.begin(), gr8.end(), m8.begin(), sum.begin(),
  26. plus<double>());
  27. cout << "sum:\t";
  28. for_each(sum.begin(), sum.end(), Show);
  29. cout << endl;
  30. vector<double> prod(LIM);
  31. transform(gr8.begin(), gr8.end(), prod.begin(),
  32. bind1st(multiplies<double>(), 2.5));
  33. cout << "prod:\t";
  34. for_each(prod.begin(), prod.end(), Show);
  35. cout << endl;
  36. // cin.get();
  37. return 0;
  38. }
  39. void Show(double v)
  40. {
  41. std::cout.width(6);
  42. std::cout << v << ' ';
  43. }