map::count
返回中元素的数目。键匹配参数指定的键映射的。
size_type count(
const Key& _Key
) const;
参数
- _Key
从映射将与元素的键值。
返回值
1,如果映射包含键匹配参数类键的元素;0,如果映射不包含带匹配键的元素。
备注
成员函数返回的元素数目在 x 范围
[lower_bound (_Key),upper_bound (_Key)
要为 0 或 1,对于映射是一个容器关联。
示例
在编译此示例与 /Wp64 标志或在 64 位平台时,编译器将生成警告 C4267。 有关该警告的更多信息,请参见 编译器警告(等级 3)C4267。
// map_count.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
int main()
{
using namespace std;
map<int, int> m1;
map<int, int>::size_type i;
typedef pair<int, int> Int_Pair;
m1.insert(Int_Pair(1, 1));
m1.insert(Int_Pair(2, 1));
m1.insert(Int_Pair(1, 4));
m1.insert(Int_Pair(2, 1));
// Keys must be unique in map, so duplicates are ignored
i = m1.count(1);
cout << "The number of elements in m1 with a sort key of 1 is: "
<< i << "." << endl;
i = m1.count(2);
cout << "The number of elements in m1 with a sort key of 2 is: "
<< i << "." << endl;
i = m1.count(3);
cout << "The number of elements in m1 with a sort key of 3 is: "
<< i << "." << endl;
}
要求
标头: <map>
命名空间: std