unordered_map count
时间: 2023-10-21 13:27:06 浏览: 57
unordered_map的count函数用于返回指定键的数量。在给定的unordered_map中,如果指定键存在,则count函数返回1,否则返回0。
unordered_map的count函数语法如下:
unordered_map_name.count(key)
其中,unordered_map_name是unordered_map的变量名,key是要查找的键值。
例如,对于以下代码片段:
unordered_map<int, char> umap;
umap = 'a';
cout << "umap.count(1) is " << umap.count(1) << "\n";
umap.erase(1);
cout << "umap.count(1) is " << umap.count(1) << "\n";
count函数将返回如下结果:
umap.count(1) is 1
umap.count(1) is 0
这说明在初始时,umap中存在键为1的键值对,因此count函数返回1。在调用erase函数后,umap中不再存在键为1的键值对,所以count函数返回0。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [unordered_map的find和count函数使用总结](https://blog.csdn.net/qq_33634666/article/details/116279227)[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: 50%"]
- *2* *3* [关于使用unordered_map.count()作为判断条件的一些坑](https://blog.csdn.net/qq_37026934/article/details/124785297)[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: 50%"]
[ .reference_list ]