fork download
  1. #include <functional>
  2. #include <iostream>
  3. #include <map>
  4.  
  5. using namespace std;
  6.  
  7. function<int(int)> memoize(function<int(int)> func)
  8. {
  9. map<int, int> results;
  10.  
  11. function<int(int)> memoized([&] (int i) {
  12.  
  13. if(results.count(i)) {
  14. return results[i];
  15. } else {
  16. int result = func(i);
  17. results.insert(pair<int, int>(i, result));
  18. return result;
  19. }
  20.  
  21. });
  22.  
  23. return memoized;
  24. }
  25.  
  26. int fib(int i)
  27. {
  28. if (i == 0 || i == 1) {
  29. return i;
  30. } else {
  31. return fib(i - 1) + fib(i - 2);
  32. }
  33. }
  34.  
  35. int main()
  36. {
  37. auto fibm = memoize(&fib);
  38. cout << fibm(23) << endl;
  39.  
  40. cin.get();
  41. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:7: error: expected constructor, destructor, or type conversion before ‘<’ token
stdout
Standard output is empty