fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. class INT {
  5. int i;
  6. public:
  7. INT(int ii) : i{ ii } {}
  8. operator int() { return i; }
  9.  
  10. INT& operator++() { ++i; return *this; }
  11. INT operator++(int) { int temp = i; ++i; return temp; } //is this the way to go?
  12.  
  13. INT& operator--() { --i; return *this; }
  14. INT operator--(int) { int temp = i; --i; return temp; } //is this the way to go?
  15. };
  16.  
  17. template<typename Iter, typename Fct>
  18. Fct for_each(Iter b, Iter e, Fct f)
  19. {
  20. while(b != e) f(*b++);
  21. return f;
  22. }
  23.  
  24. void increment(INT&i)
  25. {
  26. ++i;
  27. }
  28.  
  29. void display(INT i)
  30. {
  31. std::cout << int(i) << ' ';
  32. }
  33.  
  34. int main() {
  35. std::vector<INT> v = { 3, 4, 5 };
  36.  
  37. for_each(v.begin(), v.end(), display);
  38. std::cout << '\n';
  39.  
  40. for_each(v.begin(), v.end(), increment);
  41.  
  42. for_each(v.begin(), v.end(), display);
  43. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
3 4 5 
4 5 6