我更习惯单一的地图,而不是嵌套的地图。因此,我很难理解如何处理这个问题。
备注:请不要要求我使用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 Toronto1案例:如果国家(加拿大)在勘探结构上不存在,我加上它以及省和它的城市。
2案例:如果国家(加拿大)存在,所以我加上安大略省和多伦多长侧之前输入的数据。
欢迎任何迹象、想法或暗示。
发布于 2018-03-22 04:47:19
你的例子是非常正确的。查看下面的代码(在C++11中,但同样的推理适用于以前的版本),您可以看到每个国家只在exploration地图中插入一次。
代码:
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, map <string, string> > exploration;
exploration["Canada"]["Ontario"] = "Toronto";
exploration["Canada"]["Alberta"] = "Edmonton";
exploration["USA"]["Washington"] = "Seattle";
exploration["USA"]["Nevada"] = "Las Vegas";
cout << "Number of keys: " << exploration.size() << endl;
for (auto country : exploration) {
for (auto city : country.second)
cout << country.first << ":" << city.first << " -> " << city.second << endl;
}
return 0;
}输出:
Number of keys: 2
Canada:Alberta -> Edmonton
Canada:Ontario -> Toronto
USA:Nevada -> Las Vegas
USA:Washington -> Seattle让我们一步一步地了解正在发生的事情:
map<string, map <string, string> > exploration:定义一个映射,其键是字符串,其值是其他映射。到目前为止它是空的。exploration["Canada"]["Ontario"] = "Toronto":首先,它检查exploration map中不存在Canada。因此创建了一个映射,将Canada链接到一个空映射。现在,在Ontario映射中创建了从Toronto到exploration["Canada"]的映射。exploration["Canada"]["Alberta"] = "Edmonton":由于Canada键已经存在于exploration中,所以不需要再次创建它。它将只将Alberta映射到已经创建的映射中的Edmonton。同样的推理也适用于USA地图。这样,即使使用嵌套映射,也不会有重复的键。C++很棒。
发布于 2018-03-22 04:57:42
你可以在同一个州有两个城市,所以你真的需要map<string, map<string, set<string>>>
但你之前的一切都会奏效的。例如:
#include <iostream>
#include <map>
#include <set>
#include <string>
int main() {
std::map<std::string, std::map<std::string, std::set<std::string>>> countries;
countries["Canada"]["Ontario"].insert("Toronto");
countries["Canada"]["Ontario"].insert("Something else");
countries["Canada"]["Alberta"].insert("Edmonton");
for (const auto& country : countries) {
std::cout << "Country: " << country.first << "\n";
for (const auto& statePair : country.second) {
std::cout << "State: " << statePair.first << "\n";
for (const auto& city : statePair.second) {
std::cout << "City: " << city << "\n";
}
}
}
return 0;
}威尔输出
Country: Canada
State: Alberta
City: Edmonton
State: Ontario
City: Something else
City: Toronto编辑:
关于你的后续问题,这应该会使你达到以下目的:
using CountryMap = std::map<std::string, std::map<std::string, std::set<std::string>>>;
bool countryContainsCity(const CountryMap& countryMap, const std::string& country, const std::string& city)
{
for (const auto& country : countryMap[country]) {
for (const auto& statePair : country.second) {
if (statePair.second.find(city)
{
return true;
}
}
}
return false;
}https://stackoverflow.com/questions/49420635
复制相似问题