fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. void call_each_impl(int begin, int end, std::function<void(int)> f);
  5.  
  6. template <typename F>
  7. void call_each(int begin, int end, F f)
  8. {
  9. call_each_impl(begin, end, f);
  10. }
  11.  
  12. struct test1
  13. {
  14. void operator()(int i) { std::cout << "[1] " << i << std::endl; }
  15. };
  16.  
  17. void test2(int i)
  18. {
  19. std::cout << "[2] " << i << std::endl;
  20. }
  21.  
  22. int main() {
  23. call_each( 1, 4, test1() );
  24. call_each( 4, 7, test2 );
  25. return 0;
  26. }
  27.  
  28. void call_each_impl(int begin, int end, std::function<void(int)> f)
  29. {
  30. for (int i=begin; i<end; ++i)
  31. {
  32. f(i);
  33. }
  34. }
  35.  
  36.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
[1] 1
[1] 2
[1] 3
[2] 4
[2] 5
[2] 6