unordered_map.count函数
时间: 2023-08-17 17:11:36 浏览: 57
unordered_map.count函数是用来统计给定键值在unordered_map中出现的次数的函数。它返回一个整数,表示给定键值在unordered_map中的出现次数。如果给定键值存在,则返回1,否则返回0。
示例代码:
```cpp
#include <iostream>
#include <unordered_map>
int main() {
std::unordered_map<int, int> myMap;
// 向unordered_map中插入键值对
myMap.insert({1, 10});
myMap.insert({2, 20});
myMap.insert({3, 30});
myMap.insert({4, 40});
// 统计键值为2的出现次数
int count = myMap.count(2);
std::cout << "键值为2的出现次数为: " << count << std::endl;
// 统计键值为5的出现次数
count = myMap.count(5);
std::cout << "键值为5的出现次数为: " << count << std::endl;
return 0;
}
```
输出结果:
```
键值为2的出现次数为: 1
键值为5的出现次数为: 0
```
相关问题
unordered_map 中count函数
引用:unordered_map中的count函数是用来查找指定值在unordered_map中出现的次数的。函数的形式为 哈希表变量名.count(要查找的值)。返回值为找到的个数。例如,如果有一个unordered_map变量map,我们可以使用map.count(0)来查找键为0的键值对在map中出现的次数。如果找到了,返回值为1,否则返回值为0。
所以,如果在一个unordered_map中使用count函数,并且返回的结果大于0,就说明这个unordered_map中包含了指定值的键值对。<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_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [C++ unordered_map 和 unordered_set 常用函数](https://blog.csdn.net/qq_40876059/article/details/123699886)[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_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
c++ unordered_map.count
在C++中,`unordered_map.count`是用于统计`unordered_map`容器中某个特定键的出现次数的函数。它返回一个整数值,表示该键在容器中出现的次数。
下面是`unordered_map.count`的使用示例:
```cpp
#include <iostream>
#include <unordered_map>
int main() {
std::unordered_map<std::string, int> myMap;
myMap["apple"] = 3;
myMap["banana"] = 2;
myMap["orange"] = 5;
int count = myMap.count("apple");
std::cout << "The count of 'apple' is: " << count << std::endl;
return 0;
}
```
输出结果为:
```
The count of 'apple' is: 1
```