Search code examples
c++dictionarystructiteratormultimap

Not updating struct values in map of multimaps


I'm using C++ and have a number of data structures that I need to update things in. I use a map from double to multi maps of int and my struct. It looks like this:

std::map<double, std::multimap<int, dnode*> *> m_ptable;

My struct definition looks like this:

struct dnode{
    std::string ticker;
    double buy;
    double sell;

    //(sell/buy)
    double result;
    bool hasDuplicate;
};

The issue is that I need to iterate through all of the dnodes in the multi map and update their value of "sell" and "result" because this has to be computed after all of the nodes are already in the multi map. Here is the function I wrote to do that:

void dataProcessor::addCloseToEntry(double close){
    map<double, std::multimap<int, dnode*> *>::iterator it;
    multimap<int, dnode*>::iterator mm_it;

    for(it = m_ptable.begin(); it != m_ptable.end(); it++){
        for(mm_it = (it->second)->begin(); mm_it != (it->second)->end(); mm_it++){
            mm_it->second->sell = close;
            mm_it->second->result = close/(mm_it->second->buy);

        }
    }
    return;
}

When I step through this code I see that the values for "sell" and "result" are not changing, but remain as 0 (I initialize them to zero when I add a new node into the map.)

Is there something I'm doing incorrectly with the iterators here that's causing this?

Thanks!


Solution

  • Ok this code works fine. My logic was messed up in the lines before this. This function was called inside a while loop with a parameter that was reinitialized to zero before it was called every time.

    Wow.

    Thanks anyway guys!