map 存 list 机构
时间: 2023-10-15 18:31:15 浏览: 36
您好!要使用 C++ 的标准库中的 `map` 来存储一个机构的列表,您可以将机构的名称作为键(key),而机构的详细信息作为值(value)。以下是一个简单的示例:
```cpp
#include <iostream>
#include <map>
#include <string>
#include <vector>
struct Institution {
std::string name;
// 其他机构的详细信息...
};
int main() {
std::map<std::string, std::vector<Institution>> institutionMap;
// 添加机构到地图中
std::vector<Institution> institutions1;
institutions1.push_back({"Institution 1"});
institutions1.push_back({"Institution 2"});
institutionMap["City 1"] = institutions1;
std::vector<Institution> institutions2;
institutions2.push_back({"Institution 3"});
institutions2.push_back({"Institution 4"});
institutionMap["City 2"] = institutions2;
// 根据键访问值
std::cout << "机构列表:" << std::endl;
for (const auto& pair : institutionMap) {
std::cout << "城市: " << pair.first << std::endl;
std::cout << "机构: ";
for (const auto& institution : pair.second) {
std::cout << institution.name << ", ";
}
std::cout << std::endl;
}
return 0;
}
```
在这个示例中,我们定义了一个结构体 `Institution` 来存储机构的详细信息。然后,我们使用 `std::map` 来创建一个名为 `institutionMap` 的映射,其中键是城市的名称,而值是一个包含机构的向量。您可以根据需要修改结构体和添加更多的机构信息。
请注意,这只是一个简单的示例,您可以根据自己的需求进行调整和扩展。希望对您有帮助!如有任何问题,请随时提问。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)