fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. class Person {
  9. string name;
  10. public:
  11. Person(string n) : name(n) {}
  12. string getName() const { return name; }
  13. };
  14.  
  15. const Person* make_ptr(const Person* p) { return p; }
  16. const Person* make_ptr(const Person& p) { return &p; }
  17.  
  18. template<typename T, typename R>
  19. void xform(vector<T>& from, vector<R>& to) {
  20. transform(from.begin(), from.end(), back_inserter(to),
  21. [&](T &p) { return make_ptr(p)->getName(); }
  22. );
  23. }
  24.  
  25. int main() {
  26. Person p0("quick");
  27. Person p1("brown");
  28. Person p2("fox");
  29. Person p3("jumps");
  30. Person p4("over");
  31. Person p5("the");
  32. Person p6("lazy");
  33. Person p7("dog");
  34. vector<Person> vp;
  35. vector<Person*> vpp;
  36. vp.push_back(p0);
  37. vpp.push_back(&p0);
  38. vp.push_back(p1);
  39. vpp.push_back(&p1);
  40. vp.push_back(p2);
  41. vpp.push_back(&p2);
  42. vp.push_back(p3);
  43. vpp.push_back(&p3);
  44. vp.push_back(p4);
  45. vpp.push_back(&p4);
  46. vp.push_back(p5);
  47. vpp.push_back(&p5);
  48. vp.push_back(p6);
  49. vpp.push_back(&p6);
  50. vp.push_back(p7);
  51. vpp.push_back(&p7);
  52. vector<string> res;
  53. xform(vp, res);
  54. xform(vpp, res);
  55. for (int i = 0 ; i != res.size() ; i++) {
  56. cout << res[i] << endl;
  57. }
  58. return 0;
  59. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
quick
brown
fox
jumps
over
the
lazy
dog
quick
brown
fox
jumps
over
the
lazy
dog