C++ Memoization understanding

前端 未结 3 1880
忘了有多久
忘了有多久 2020-12-22 02:01

I was trying to understand how memoization works in C++, so I looked at an example of memoization used in Fib. sequence.

std::map fibHash;

         


        
3条回答
  •  春和景丽
    2020-12-22 02:55

    The code indeed save each fib_val into the fibHash map. The find method called on fibHash searches the map to see if the value was previously computed. If so, find returns an iterator on this value and the function returns it (return *fibIter).

    fibHash[ n ] = fib_val; adds a new value in the map.

提交回复
热议问题