unordered_map如何输出
时间: 2023-10-20 16:33:11 浏览: 76
unordered_map可以使用迭代器来输出。通过遍历unordered_map的每个元素,可以逐个打印出键值对。下面是一个示例代码:
```cpp
std::unordered_map<std::string, int> umap;
umap.insert(std::make_pair("apple", 5));
umap.insert(std::make_pair("banana", 3));
umap.insert(std::make_pair("orange", 2));
for(auto it = umap.begin(); it != umap.end(); ++it) {
std::cout << "Key: " << it->first << ", Value: " << it->second << std::endl;
}
```
这段代码会输出unordered_map中每个键值对的键和值。注意,unordered_map的迭代器按照哈希表的顺序遍历,而不是插入的顺序。因此,输出的顺序可能与插入的顺序不一致。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [C++中的unordered_map用法详解](https://blog.csdn.net/web15286201346/article/details/123614717)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文