【发布时间】:2023-04-10 13:42:01
【问题描述】:
如何在 C++ 中通过 std::map 循环、访问和分配?我的地图定义为:
std::map<std::string, std::map<std::string, int>>
例如,上面的容器保存这样的数据:
m["country"]["capital"] = value;
m["country1"]["capital1"] = value;
m["country2"]["capital2"] = value;
根据国家和首都,天气值得到更新
目前是否使用and map
map<string,int>mp;
if(Albania)
map[Tirana]= weathervalue;
if("Algeria")
map["Algiers"] = weathervalue;
欢迎任何优化提示、指示和想法
【问题讨论】:
-
欢迎来到stackoverflow!您能否详细说明第一种和第二种地图类型是如何一起使用的?第二个示例具有
int值,但在第二个示例中,它是std::string? -
抱歉错字,它实际上是 std::map >
-
输入数据从何而来?字符串和整数?
-
weather(int)和capital(string)是从底层获取的。例如,如果从下层收到的国家是阿尔及利亚,则为该国家的首都(本地)分配天气值(从下层)
-
好的,但是作为什么?
std::vector<std::string>和std::vector<int>?您要求在地图上进行循环,但我认为我们还需要知道要放入地图的数据所在的位置,以便回答您的问题。
标签: c++ dictionary mapping nested-map