共用方式為


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

標準樣板程式庫