fork download
  1. #include <functional>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <string>
  5. #include <iostream>
  6. #include <iterator>
  7. using namespace std;
  8.  
  9. template<typename F1, typename F2>
  10. class unary_composer
  11. {
  12. F1 f1;
  13. F2 f2;
  14.  
  15. public:
  16. unary_composer(F1 lf1, F2 lf2) : f1(lf1), f2(lf2){}
  17.  
  18. template <typename ARG>
  19. auto operator()(ARG x)->decltype(f1(f2(x))) { return f1(f2(x)); }
  20. };
  21.  
  22. template <typename F1, typename F2>
  23. unary_composer<F1, F2> compose(F1 fone, F2 ftwo)
  24. {
  25. return unary_composer<F1, F2>(fone, ftwo);
  26. }
  27.  
  28. int main()
  29. {
  30. const int SZ = 6;
  31. vector<string> vs{"1.2", "3.4", "5.6", "6.7", "7.8", "8.9"};
  32. vector<double> vd;
  33.  
  34. transform(vs.begin(), vs.end(), back_inserter(vd), compose(std::atof, mem_fn(&string::c_str)));
  35.  
  36. copy(vd.begin(), vd.end(), ostream_iterator<double>(cout, " ")); // print to console
  37. cout<<endl;
  38.  
  39. }
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
1.2 3.4 5.6 6.7 7.8 8.9