我更习惯于单个地图而不是嵌套地图。所以我很难理解如何处理这个问题。
备注:请不要要求我使用 multimap 或 boost,我被限制使用这种结构
在 C++ STL 映射中,我有这样的定义
map<string, map<string, string>> exploration; // country --> state --> city
第一张地图将一对键(国家)和值表示为树结构map<string, string>
,它们代表各自的州和城市。
我知道如何手动填充这个结构(硬编码),如下所示:
exploration["Canada"]["Ontario"] = "Toronto";
问题 :
稍后,上面显示的数据将由用户输入:
> Canada Ontario Toronto
> Canada Alberta Edmonton
> USA Washington Seatle
> USA Nevada Las-Vegas
因此,如果我将此逻辑应用于上面的示例,这是错误的,因为 map 不接受重复的键
插图(错误):
exploration["Canada"]["Ontario"] = "Toronto";
exploration["Canada"]["Alberta"] = "Edmonton";
插图(我在找什么):
exploration["Canada"]["Ontario"] = "Toronto";
["Alberta"] = "Edmonton";
Canada
**
* *
* *
Ontario Alberta
* *
* *
Toronto Edmonton
我假设的解决方案,
> Canada Ontario Toronto
1 案例:如果勘探结构中不存在国家(加拿大),我添加它以及省及其城市。
2 案例:如果国家(加拿大)存在,那么我在之前输入的数据中添加安大略和多伦多的长边。
欢迎任何指示、想法或提示。