fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <functional>
  4.  
  5. class Foo
  6. {
  7. public:
  8. Foo() {
  9. arry[0] = "foo";
  10. arry[1] = "bar";
  11. arry[2] = "baz";
  12. arry[3] = "qux";
  13. }
  14.  
  15. // template <typename T>
  16. // void each(T func)
  17. // void each(const std::function<std::string (std::string ele)> &func)
  18. void each(std::string (* func)(std::string ele))
  19. {
  20. for(int i = 0; i < size; i++) {
  21. func(arry[i]);
  22. }
  23. }
  24.  
  25. int size = 4;
  26. std::string arry[4];
  27. };
  28.  
  29. int main(void)
  30. {
  31. Foo foo;
  32.  
  33. foo.each(
  34. [](std::string str) -> std::string {
  35. std::cout << str << std::endl;
  36. return str;
  37. }
  38. );
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
foo
bar
baz
qux