c++ 嵌套map遍历
时间: 2023-12-22 14:27:39 浏览: 186
以下是C++中嵌套map的遍历方法:
```cpp
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, map<int, string>> nestedMap;
nestedMap[0][0] = "hello";
nestedMap[0][1] = "world";
nestedMap[1][0] = "foo";
nestedMap[1][1] = "bar";
for (auto& outerPair : nestedMap) {
int outerKey = outerPair.first;
map<int, string>& innerMap = outerPair.second; for (auto& innerPair : innerMap) {
int innerKey = innerPair.first;
string value = innerPair.second;
cout << "outer key: " << outerKey << ", inner key: " << innerKey << ", value: " << value << endl;
}
}
return 0;
}
```
输出结果为:
```
outer key: 0, inner key: 0, value: hello
outer key: 0, inner key: 1, value: world
outer key: 1, inner key: 0, value: foo
outer key: 1, inner key: 1, value: bar
```
阅读全文