![]() |
---|
此 API 已过时。另一种方法是 unordered_map Class。 |
返回元素数。键匹配一个参数指定键的 hash_map 的。
size_type count(
const Key& _Key
) const;
参数
- _Key
从 hash_map 将与元素的键值。
返回值
1,如果 hash_map 包含排序关键字参数匹配键的元素;0,如果 hash_map 不包含具有匹配键的元素。
备注
成员函数返回的元素数 x 在范围
[lower_bound (_Key )、upper_bound (_Key ) )
哪些为 0 或 1 后 hash_map,是一个关联容器。
在 Visual C++ .NET 2003 中,<hash_map> 和 <hash_set> 标头文件的成员中不再标准,命名空间,而是将 stdext 命名空间。有关更多信息,请参见 stdext 命名空间。
示例
当编译此示例与 /Wp64 标记或在 64 位平台时,警告的编译器 C4267 将生成。有关此警告的更多信息,请参见 编译器警告(等级 3)C4267。
// hash_map_count.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main()
{
using namespace std;
using namespace stdext;
hash_map<int, int> hm1;
hash_map<int, int>::size_type i;
typedef pair<int, int> Int_Pair;
hm1.insert(Int_Pair (1, 1));
hm1.insert(Int_Pair (2, 1));
hm1.insert(Int_Pair (1, 4));
hm1.insert(Int_Pair (2, 1));
// Keys must be unique in hash_map, so duplicates are ignored
i = hm1.count(1);
cout << "The number of elements in hm1 with a sort key of 1 is: "
<< i << "." << endl;
i = hm1.count(2);
cout << "The number of elements in hm1 with a sort key of 2 is: "
<< i << "." << endl;
i = hm1.count(3);
cout << "The number of elements in hm1 with a sort key of 3 is: "
<< i << "." << endl;
}
要求
标头: <hash_map>
命名空间: stdext