How to pass map by reference during Memoization?
*Passing map by pointer works fine but I need to keep it on Stack any idea how can I achieve that.
first method fib works properly but the function fibRef not working the map not accepting the pair.
I could understand it's something related to RValue and LValue.
*
//
//  Fib.cpp
// 
//
//  Created by Youssef Hanna on 1/26/21.
//
#include <stdio.h>
#include <iostream>
#include <map>
// works fine
long long fib(int n, std::map<int, long long> *values = new std::map<int , long long>())
{
  if (n <= 2 )
    return n;
    
    if(values->find(n) != values->end())
        return values->at(n);
    
    long long result =  fib(n-1,values) + fib(n-2,values);
    values->insert(std::pair<int, long long>(n,result));
    
    
    return result;
}
// Error values not added to the map
int fibRef(int n, std::map<int, int> values = {})
{
  if (n <= 2)
    return 1;
    
    if(values.find(n) != values.end())
        return values.at(n);
    
    int result =  fibRef(n-1,values) + fibRef(n-2,values);
    values.insert(std::pair<int, int>(n,result));
    
    
    return result;
}
// Best option at the moment 
long long fibWithPassingMap(int n, std::map<int, long long> & values)
{
  if (n <= 2)
    return 1;
    
    if(values.find(n) != values.end())
        return values.at(n);
    
    long long result =  fibWithPassingMap(n-1,values) + fibWithPassingMap(n-2,values);
    values.insert(std::pair<int, long long>(n,result));
    
    
    return result;
}
void runFibTest(){
    
    
    
    std::cout<<"\n"<<fib(10) << "\n" ;
    std::cout<<"\n"<<fib(20) << "\n" ;
    std::cout<<"\n"<<fib(30) << "\n" ;
    std::cout<<"\n"<<fib(100) << "\n" ;
    std::cout<<"\n"<<fib(250) << "\n" ;
    std::cout<<"\n"<<fib(1000) << "\n" ;
   
    std::map<int , long long> map;
    std::cout<<"\n"<<fibWithPassingMap(100, map) << "\n" ;
    std::cout<<"\n"<<fibWithPassingMap(90, map) << "\n" ;
    std::cout<<"\n"<<fibWithPassingMap(25, map) << "\n" ;
    std::cout<<"\n"<<fibWithPassingMap(30, map) << "\n" ;
    std::cout<<"\n"<<fibWithPassingMap(1020, map) << "\n" ;
    map.clear();
    
    std::cout<<"\n"<<fibRef(100) << "\n" ;  // too long because memoization not working
    
    
    
}
Thanks in advance
