C++如何遍历多维map
时间: 2023-09-07 19:16:27 浏览: 170
c++大纲资料.txt
遍历多维map需要使用嵌套的迭代器。具体的操作可以参考以下代码示例:
```cpp
#include <map>
#include <iostream>
int main() {
std::map<std::string, std::map<std::string, std::string>> a = {
{"key1", {{"subkey1", "value1"}, {"subkey2", "value2"}}},
{"key2", {{"subkey3", "value3"}, {"subkey4", "value4"}}}
};
for(auto it = a.begin(); it != a.end(); ++it) {
std::cout << it->first << ":" << std::endl;
for(auto subit = it->second.begin(); subit != it->second.end(); ++subit) {
std::cout << "[" << subit->first << "]" << "[" << subit->second << "]" << std::endl;
}
}
}
```
上述代码中,我们使用了两个迭代器`it`和`subit`来分别遍历外层map和内层map。首先,外层迭代器`it`遍历外层map的键值对,然后内层迭代器`subit`遍历内层map的键值对。通过嵌套的迭代器,我们可以完成对多维map的遍历操作。<em>1</em><em>2</em><em>3</em>
#### 引用[.reference_title]
- *1* *2* *3* [C++ 二维、三维 map 的使用](https://blog.csdn.net/m0_67403013/article/details/123865013)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item]
[ .reference_list ]
阅读全文