unordered_map 个不同的自定义类型
unordered_map of different custom types
假设我有这两个enums
:
enum Type
{
Type1,
Type2
};
enum Location
{
Location1,
Location2,
Location3
};
现在我想要一个可以引用的容器,例如 container[Type1][Location1] = 5;
我不需要对元素进行排序,但我需要能够重复,例如 container[Type1]
可以是 Location1
或 Location2
等
我正在考虑使用 unordered_multimap<pair<Type, Location>, unsigned int>>
提供我想要的东西,但不允许我访问所描述的元素(或者至少我不知道该怎么做)
你有什么建议?
我相信您正在寻找嵌套地图:
std::unordered_map<Type, std::unordered_map<Location, unsigned int>>
假设我有这两个enums
:
enum Type
{
Type1,
Type2
};
enum Location
{
Location1,
Location2,
Location3
};
现在我想要一个可以引用的容器,例如 container[Type1][Location1] = 5;
我不需要对元素进行排序,但我需要能够重复,例如 container[Type1]
可以是 Location1
或 Location2
等
我正在考虑使用 unordered_multimap<pair<Type, Location>, unsigned int>>
提供我想要的东西,但不允许我访问所描述的元素(或者至少我不知道该怎么做)
你有什么建议?
我相信您正在寻找嵌套地图:
std::unordered_map<Type, std::unordered_map<Location, unsigned int>>