2012-04-23 58 views
0

我有一個std ::地圖對象如下更新的std ::地圖對象數據

typedef std::map<int,int> RoutingTable; 
RoutingTable rtable; 

,然後我在一個函數初始化它

for (int i=0; i<getNumNodes(); i++) 
{ 
    int gateIndex = parentModuleGate->getIndex(); 
    int address = topo->getNode(i)->getModule()->par("address"); 
    rtable[address] = gateIndex; 
    } 

現在我想改變另一個函數中的rtable中的值。我怎麼能做到這一點?

回答

2

通過引用傳遞rtable

void some_func(std::map<int, int>& a_rtable) 
{ 
    // Either iterate over each entry in the map and 
    // perform some modification to its values. 
    for (std::map<int, int>::iterator i = a_rtable.begin(); 
     i != a_rtable.end(); 
     i++) 
    { 
     i->second = ...; 
    } 

    // Or just directly access the necessary values for 
    // modification. 
    a_rtable[0] = ...; // Note this would add entry for '0' if one 
         // did not exist so you could use 
         // std::map::find() (or similar) to avoid new 
         // entry creation. 

    std::map<int, int>::iterator i = a_rtable.find(5); 
    if (i != a_rtable.end()) 
    { 
     i->second = ...; 
    } 
} 
+0

實際上rtable屬於一類,它是一種試圖修改數據的memberfunction。 (我想編輯現有的數據,我不想添加新的數據),但是當我嘗試這樣做時,它只是附加到rtable。我需要的是改變現有的價值觀。 :( – user1235343 2012-04-23 23:35:27

+0

@ user1235343,更新了答案。希望能爲你解決它嗎? – hmjd 2012-04-23 23:39:57

+0

是的,謝謝:) – user1235343 2012-04-24 00:15:53