fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. struct Data {
  5. size_t d_size;
  6. double *d_data;
  7. Data(size_t size) : d_size(size), d_data(new double[size]) {}
  8. ~Data() { delete[] d_data; }
  9. double &operator[](size_t i) { return d_data[i]; }
  10.  
  11. template<typename Lambda>
  12. inline void forEach(Lambda f) {
  13. for (size_t index = 0; index != d_size; ++index)
  14. f(*this, index);
  15. }
  16. };
  17.  
  18.  
  19.  
  20. int main() {
  21. Data im(50000000);
  22. im.forEach([](Data &im, size_t i) {
  23. im[i] = static_cast<double>(i);
  24. });
  25.  
  26. double sum = 0;
  27. im.forEach([&sum](Data &im, size_t i) {
  28. sum += im[i];
  29. });
  30.  
  31. std::cout << sum << '\n';
  32. }
Success #stdin #stdout 0.47s 3296KB
stdin
Standard input is empty
stdout
1.25e+15