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