我们正在用 C++ 为学校开发一个游戏项目。我负责地图对象,其中将包含炸弹、玩家、墙壁和盒子等实体。我的地图中有 3 个容器:
- 玩家的 std::list(多个玩家可以站在同一个箱子上)。
- A
std::unordered_map<int, std::unordered_map<int, AEntity*> >
用于墙壁。 - 另一个
std::unordered_map<int, std::unordered_map<int, AEntity*> >
用于炸弹+盒子。
目的是在地图上非常快速地访问实体,实体的数量可能非常多。
这就是我想到 unordered_maps 的原因,我打算这样使用它们:
Unordered_map A contain:
KEY: y coord of the Entity || VALUE: pointer to unordered_map B
Unordered_map B contain:
KEY: x coord of the entity || VALUE: pointer to the AEntity instance
问题一:
首先,对于这种用法,unordered_maps 是否那么快?我对 unordered_maps 很陌生。
问题2:
其次,这是添加元素的可行方法吗?
int main(int ac, char **av)
{
std::unordered_map <int, unordered_map <int, char*>> umap;
(umap[4])[2] = "salut";
std::cout << (umap[4])[2] << std::endl;
}