如何在C++的多维地图中实现常量正确性

How to achieve const-correctness in multidimensional map in C++

本文关键字:地图 实现 常量 正确性 C++      更新时间:2023-10-16

我有一个复杂的map,最后存储指向Drawable对象的指针。Drawable对象都有一个声明为constdraw()成员函数。我需要为地图中存储的某种类型的所有对象调用所有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.