如何在C++的多维地图中实现常量正确性
How to achieve const-correctness in multidimensional map in C++
我有一个复杂的map
,最后存储指向Drawable
对象的指针。Drawable
对象都有一个声明为const
的draw()
成员函数。我需要为地图中存储的某种类型的所有对象调用所有draw
函数,并且必须在const
函数中执行此操作。但是,我似乎无法保持我的函数的恒定正确性(drawSolid
)。
我的外部地图(map<int, ##>
)本质上是索引一些子地图。子映射又是索引向量(map<ItemType, vector<##> >
)。最后,这个向量保留一组shared_ptr<Drawable>
对象。
如果我从函数头中删除const
限定符,一切都会编译,但我需要它const
。如何遍历我的多维地图,保持恒常性正确性?
void DrawableItems::drawSolid(int item_list = -1) const
{
typedef std::map<int, std::map<ItemType, std::vector<std::shared_ptr<Drawable> > > > drawablemap;
std::vector<std::shared_ptr<Drawable> > its;
for(drawablemap::const_iterator li = __items.begin(); li != __items.end(); li++) {
its = li->second[SOLID];
for(auto di = its.begin(); di != its.end(); di++) {
di->get()->draw();
}
}
}
这是我从编译器(G ++)得到的错误:
/.../dss-sim/src/graphics/DrawableItems.cpp: In member function ‘void DrawableItems::drawSolid(int) const’:
/.../dss-sim/src/graphics/DrawableItems.cpp:51:35: error: passing ‘const std::map<DrawableItems::ItemType, std::vector<std::shared_ptr<Drawable> > >’ as ‘this’ argument discards qualifiers [-fpermissive]
its = li->second[SOLID];
^
In file included from /usr/include/c++/5/map:61:0,
from /.../dss-sim/src/common/dss.hpp:11,
from /.../dss-sim/src/graphics/DrawableItems.hpp:19,
from /.../dss-sim/src/graphics/DrawableItems.cpp:15:
/usr/include/c++/5/bits/stl_map.h:494:7: note: in call to ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = DrawableItems::ItemType; _Tp = std::vector<std::shared_ptr<Drawable> >; _Compare = std::less<DrawableItems::ItemType>; _Alloc = std::allocator<std::pair<const DrawableItems::ItemType, std::vector<std::shared_ptr<Drawable> > > >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = std::vector<std::shared_ptr<Drawable> >; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = DrawableItems::ItemType]’
operator[](key_type&& __k)
std::map
中没有operator[]
的 const 版本。但是,您可以使用 const 版本的at()
代替:
its = li->second.at(SOLID);
原因是如果还没有元素,operator[]
会插入一个元素,所以不可能有const
版本的operator[]
。
另一方面,如果不存在元素,at()
会引发异常,这与const std::map
.
相关文章:
- 如果没有malloc,链表实现将失败
- 如何在c++中实现处理器调度模拟器
- 为什么不;名字在地图上是按顺序排列的吗
- 如何在c++中使用引用实现类似python的行为
- 实现无开销push_back的最佳方法是什么
- 基于多个条件处理地图中的所有元素
- 为什么地图需要实现'operator<'以及如何比较对象?
- 如何在C++的多维地图中实现常量正确性
- 通过地图实现树种
- 模板化地图/字典实现
- 打印出使用地图实现的Trie中的所有单词
- 使用C++模板实现 Haskell 的"地图"功能时出现问题
- 在C 中实现地图向量
- 如何以不同的数据类型作为值实现地图
- 田野不能被重新爱,虽然它显然在那里!(实现地图.h)
- 实现通用地图 [来自硬件]
- 如何使用范围 v3 范围实现平面地图
- 如何在将多维 int 数组映射到 C++ 中的字符串时实现在地图上查找或计数
- 如何使用Google Protobuf实现地图结构
- 你会如何实现双向地图之类的东西