[ create a new paste ] login | about

Link: http://codepad.org/iJ97PPc4    [ raw code | output | fork ]

C++, pasted on Dec 10:
#include <iostream>
#include <unordered_map>
#include <functional>
#include <memory>

typedef std::function<int(int)> fib_func;


class Fibonacci
{

    typedef std::pair<int, int> pair;

	std::unordered_map<int, int> memo;

public:

    Fibonacci()
    {
        memo.insert(pair(0, 0));
	    memo.insert(pair(1, 1));  
    }

    int operator()(int n)
    {
        auto it = memo.find(n);
		int result;

		if (it == memo.end()) {
			result = operator() (n - 1) + operator() (n - 2);
			memo[n] = result;
		} else {
			result = it->second;
		}

		return result;
    }
};

int main()
{
    Fibonacci fibonacci;
	std::cout << fibonacci(13) << std::endl;

	std::cin.get();
}


Output:
1
2
3
Line 24: error: unordered_map: No such file or directory
Line 6: error: expected initializer before '<' token
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: