I am having trouble with recursively modifying a <string, int> map (NCPB) -- when I call for what should be existing values in the map, I get "junk" values back (integers, but sometimes negative, large numbers, etc.). I've posted only the problematic function here:
int Count_Noncrossing(string RNA, map<string, int> &NCPB)
{
map <string, int>::iterator it;
if (RNA.length() <= 2)//perfect short interval can only have 1 match; return 1
{
return 1;
}
it = NCPB.find(RNA);
if (it != NCPB.end())
{
return it->second; //return it if already calculated
}
//Determine legal places to "split" RNA
vector <int> splits;
for (int j = 1; j<(int)RNA.length(); j=j+2)
{
if (Perfect_Interval (RNA.substr(j+1)) && Bonding (RNA.substr(0,1), RNA.substr(j,1)))
{
splits.push_back(j);
}
}
//Calculate Noncrossing Matches over each subinterval
it = NCPB.find(RNA);
int sum = 0;
if (it == NCPB.end()) {sum = 0;}
else sum = it->second;
for (int j = 0; j<(int)splits.size(); ++j)
{
int first_part = Count_Noncrossing(RNA.substr(1,splits[j]-1), NCPB);
int second_part = Count_Noncrossing(RNA.substr(splits[j]+1), NCPB);
sum += first_part * second_part;
}
NCPB [RNA] = sum;
return it->second;
}
The problem is that when I ask for existing map values in a subsequent recursive call, they don't seem to be there. I imagine I'm missing something straightforward but can't seem to find it. Anybody have any ideas? I've tried declaring the map globally, passing it (as shown above), nothing seems to help. Thanks in advance!