fork download
  1. #include <iostream>
  2. #include <functional>
  3. #include <array>
  4.  
  5. template <class T, size_t N>
  6. void foreach(std::array<T, N> &arr, std::function<void(T&)> fun)
  7. {
  8. for(auto &item: arr)
  9. {
  10. fun(item);
  11. }
  12. }
  13.  
  14. int main()
  15. {
  16. std::array<int, 10> a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  17. int y = 2;
  18. std::function<void(int&)> f = [=](int &x) { x = x * y; };
  19. foreach(a, f);
  20. for(const auto &item: a)
  21. {
  22. std::cout << item << " ";
  23. }
  24. std::cout << std::endl;
  25. }
  26.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
0 2 4 6 8 10 12 14 16 18