unordered_map 里.count是什么
时间: 2023-10-14 21:26:41 浏览: 36
`.count()` 是 C++ STL 中 `unordered_map` 容器提供的一个成员函数,它用于统计特定键值在容器中出现的次数。即它返回一个整数,表示容器中键值等于给定键值的元素个数。如果该键值不存在,则返回 0。
以下是 `.count()` 的使用示例:
```c++
#include <iostream>
#include <unordered_map>
int main() {
std::unordered_map<std::string, int> m = {{"apple", 1}, {"banana", 2}, {"orange", 3}};
std::cout << "apple count: " << m.count("apple") << '\n';
std::cout << "kiwi count: " << m.count("kiwi") << '\n';
return 0;
}
```
输出为:
```
apple count: 1
kiwi count: 0
```
相关问题
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
```
unordered_map.count
unordered_map.count是一个函数,用于统计unordered_map中某个键值对的个数。该函数的形式为unordered_map变量名.count(要查找的键值)。返回值是找到的个数。例如,如果有一个unordered_map<int, int> map,其中map<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* [unordered_map中find和count函数的区别](https://blog.csdn.net/qq_44879626/article/details/116192494)[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 style="max-width: 50%"]
[ .reference_list ]