如何创建地图的地图
How do you create a map of maps?
我想做以下事情:
.h
map<int, map<int,int> > forwardingTable;
.cpp
int
UpdateForwardingTable(int dest, int hop, int cost)
{
if(forwardingTable.at(dest) != forwardingTable.end())
forwardingTable.at(dest) = make_pair(hop, cost);
else
forwardingTable.insert(dest, make_pair(hop, cost));
}
但是我得到了无数的编译错误,类似于:
In file included from /usr/include/c++/4.8/map:60:0,
from globals.h:25,
from rtngnode.h:2,
from rtngnode.cpp:1:
/usr/include/c++/4.8/bits/stl_tree.h:316:5: note: template<class _Val> bool std::operator!=(const std::_Rb_tree_iterator<_Tp>&, const std::_Rb_tree_const_iterator<_Val>&)
operator!=(const _Rb_tree_iterator<_Val>& __x,
^
/usr/include/c++/4.8/bits/stl_tree.h:316:5: note: template argument deduction/substitution failed:
rtngnode.cpp:205:53: note: ‘std::map<int, std::map<int, int, std::less<int> > >::mapped_type {aka std::map<int, int, std::less<int> >}’ is not derived from ‘const std::_Rb_tree_iterator<_Tp>’
if(forwardingTable.at(dest) != forwardingTable.end())
我做错了什么吗?有更好的容器来装这种东西吗?
有两个问题:
1, make_pair
返回pair
,而不是map
。
2, at(dest)
可能抛出out_of_range异常,参考map::at
应该是:
int
UpdateForwardingTable(int dest, int hop, int cost)
{
map<int, map<int,int> >::iterator itr = forwardingTable.find(dest);
if(itr != forwardingTable.end())
{
itr->second.insert(hop, cost);
// forwardingTable.at(dest) = make_pair(hop, cost);
}
else
{
map<int, int> obj;
obj.insert(hop, const);
forwardingTable.insert(dest, obj);
// forwardingTable.insert(dest, make_pair(hop, cost));
}
}
相关文章:
- 在 c++ 中,有一种方法可以创建一个包含地图作为值的树状地图?
- 无法创建带有布尔值和矢量的地图
- 我可以动态创建新地图并作为函数参数传递吗?
- C++ 如何为地图创建浅拷贝构造函数
- 如何在给定任意数量的整数的情况下创建一个唯一键?并使用该键存储,然后从地图中查找
- 从两个地图创建set_difference矢量
- 如何从OpenGL 4.5中的单个Cube地图纹理中创建Cube地图数组纹理
- 如何在两个包含指针的地图中创建列表
- std::unordered_map 如何释放使用 malloc 创建的结构.是否需要对地图进行 2 次查询
- 是 std::make_pair 在将对象添加到地图时创建副本
- Boost HANA:从集合创建地图和默认值
- 如何以自定义类/比较器作为密钥创建地图
- 使用struct成员抛出bad_alloc创建地图
- 从C 中的MAP的随机子集创建新地图
- 从无序地图创建以自定义类作为键的地图.但是,由于const而无法使用的要素
- 在C 中创建一个巨大的标志地图
- 如何创建结构类型数组的地图对象
- 在Cython的循环中创建C 地图
- C Boost ::如何从顶点描述符创建地图
- C++从地图创建排序的矢量