map与unordered_map,count() 与find()

map与unordered_map,count() 与find()

使用count,返回的是被查找元素的个数。如果有,返回1;否则,返回0。注意,map中不存在相同元素,所以返回值只能是1或0。

使用find,返回的是被查找元素的位置,没有则返回map.end()。

map与unordered_map相比:

map底层实现为红黑数,undered_map底层实现为哈希表,两者均不能有重复的建,均支持[]运算符

map与multimap相比:

两者底层实现均为红黑树,但是multimap支持重复的键,不支持[]运算

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
unordered_mapcount函数用于返回指定键的数量。在给定的unordered_map中,如果指定键存在,则count函数返回1,否则返回0。 unordered_mapcount函数语法如下: 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 ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值