简体   繁体   English

不更新多图映射中的结构值

[英]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. 我使用的是C ++,并且有很多数据结构需要更新。我使用从int和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. 问题是我需要遍历多重映射中的所有dnode并更新其“ sell”和“ result”的值,因为必须在所有节点都已经存在于多重映射中之后才进行计算。 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.) 当我逐步执行此代码时,我看到“ sell”和“ result”的值没有改变,而是保持为0(将新节点添加到地图中时,我将它们初始化为零。)

Is there something I'm doing incorrectly with the iterators here that's causing this? 我是否在使用迭代器做错了某些事情,从而导致这种情况?

Thanks! 谢谢!

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. 在while循环内调用了此函数,并在每次调用前将其参数重新初始化为零。

Wow. 哇。

Thanks anyway guys! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM