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

请参见

参考

map Class

标准模板库