c++ map中存放map
时间: 2023-12-28 15:25:44 浏览: 139
在C++中,可以使用嵌套的map来存放map。下面是一个示例代码:
```cpp
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
map<string, map<string, int>> my_map;
// 添加数据到嵌套的map中
my_map["map1"]["key1"] = 1;
my_map["map1"]["key2"] = 2;
my_map["map2"]["key3"] = 3;
my_map["map2"]["key4"] = 4;
// 遍历并输出嵌套的map中的数据
for (auto it1 = my_map.begin(); it1 != my_map.end(); ++it1) {
cout << "Map: " << it1->first << endl;
for (auto it2 = it1->second.begin(); it2 != it1->second.end(); ++it2) {
cout << "Key: " << it2->first << ", Value: " << it2->second << endl;
}
}
return 0;
}
```
输出结果为:
```
Map: map1
Key: key1, Value: 1
Key: key2, Value: 2
Map: map2
Key: key3, Value: 3
Key: key4, Value: 4
```
阅读全文