我有一个充当虚拟路由器的程序...我将转发表存储在地图的地图中:map<int, map<int,int> >
---
在我正在尝试的呼叫之前,以下是我的转发表的当前状态,已在之前初始化:
//destination | hop, cost
map<int, map<int,int> > forwardingTable;
forwardingTable[3701][3701] = 8;
现在我正在尝试执行以下操作:
//iterators and pathCost are initialized earlier
forwardingTable[topoIt->first][*pIter] = pathCost;
cout << "forwardingTable[" << topoIt->first << "][" << *pIter << "] = " << pathCost << endl;
有了这个,我得到了输出:
forwardingTable[3701][3705] = 6
但是,在同一方法中,当我遍历转发表时:
forwardingTableIter iter = forwardingTable.begin();
while(iter != forwardingTable.end())
{
map<int,int, less<int> >::iterator inner_it = iter->second.begin();
cout << "--Dest: " << iter->first << "\tHop: " << inner_it->first << "\tCost: " << inner_it->second << endl;
iter++;
}
当它到达节点3701时,我得到这个输出:
//still hop 3701, cost of 8
--Dest: 3701 Hop: 3701 Cost: 8
我在这里误解了[] []运算符吗?我的印象是它会搜索引用的元素,如果找到,则更新它 - 否则,插入它。
答案 0 :(得分:1)
请注意3705
。
forwardingTable[3701][3705] = 6