fork(2) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. struct ha {
  8. int i;
  9. explicit ha(int a) : i(a) {}
  10. };
  11.  
  12. int main()
  13. {
  14. vector<ha> v{ ha{1}, ha{7}, ha{1} }; // initial vector
  15. // GOAL : make a vector of pointers to elements with i < 2
  16. vector<ha*> ph; // target vector
  17. vector<ha*> pv; // temporary vector
  18. // 1.
  19. transform(v.begin(), v.end(), back_inserter(pv),
  20. [](ha &arg) { return &arg; });
  21. // 2.
  22. copy_if(pv.begin(), pv.end(), back_inserter(ph),
  23. [](ha *parg) { return parg->i < 2; }); // 2.
  24.  
  25. return 0;
  26. }
  27.  
  28.  
Success #stdin #stdout 0s 3424KB
stdin
Standard input is empty
stdout
Standard output is empty