fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. template <
  8. class InputIterator, class OutputIterator,
  9. class UnaryOperator, class Pred
  10. >
  11. OutputIterator transform_if(InputIterator first1, InputIterator last1,
  12. OutputIterator result, UnaryOperator op, Pred pred)
  13. {
  14. while (first1 != last1)
  15. {
  16. if (pred(*first1)) {
  17. *result = op(*first1);
  18. ++result;
  19. }
  20. ++first1;
  21. }
  22. return result;
  23. }
  24.  
  25. struct ha {
  26. int i;
  27. explicit ha(int a) : i(a) {}
  28. };
  29.  
  30. int main()
  31. {
  32. vector<ha> v{ ha{1}, ha{7}, ha{1} }; // initial vector
  33. // GOAL : make a vector of pointers to elements with i < 2
  34. vector<ha*> ph; // target vector
  35.  
  36. // example call
  37. transform_if(v.begin(), v.end(), back_inserter(ph),
  38. [](ha &arg) { return &arg; }, // 1.
  39. [](ha &arg) { return arg.i < 2; });// 2.
  40.  
  41. return 0;
  42. }
  43.  
  44.  
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
Standard output is empty