在类型上使用运算符 [][] 时,不会更新值:map<int、map<int、int> >
Values aren't updated when using operator[][] on type: map<int, map<int, int> >
我有一个程序,作为一种虚拟路由器…我将转发表存储在映射的映射中: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
我误解了[][]运算符了吗?我的印象是,它会搜索引用的元素,如果找到,更新它-否则,插入它。
注意3705
forwardingTable[3701][3705] = 6
相关文章:
- 如何使用 uint64_t 键类型从 std::map<int, std::string> 返回值?
- 类型为 'std::map< char,int > 的 <Swig 对象的代理
- 如何为地图< map<int,int> 、bool > 分配值?
- map<string, int> m= {}; 在C++中是什么意思?
- 如何从map<pair<string,int>,pair<string,Array>>中迭代和查找?
- 尝试对 std::map<int, class> 进行排序时出错
- 在变量名后声明带有 () 的非内部类型与不使用变量名的行为不同。即 std::map<int,char>x(); - 这是怎么回事?
- freq_table[field_pm_value] = map<string, int>(); 是什么意思?
- 如何在 std::map<const int、int> C++ 中重载插入运算符>>?
- map int<-->int, 1:N, 已知边界
- 使用 pair<int、int> 或字符串作为 map<> 键,哪个更有效?
- map<int,int>的*迭代器是什么?它不是pair<int,int>
- 方括号在"map <string, int> instance[numberFeatures];"中是什么意思
- std::map<int, A> 运算符 [] 需要使用空构造函数创建 A
- 如何仅根据第二个元素对map<int,pair<int,int> >进行排序?
- 如何在C++中打印矢量<map<string,int> >
- 最有效的安全方法将 std::map<int, std::shared_ptr> 转换为 std::<Base>map<int, std::shared_ptr<D
- 如何为包含map<int,*double>成员的类编写析构函数?
- std::multiset vs. std::<int>map<int, std::size_t> 用于保留多个可重复的整数值
- 将 std::map<int, vector> 复制到 std::<int>map<std:string, vector<int>>