共用方式為


hash_map::count

注意事項注意事項

這個 API 已經過時。這個選項是 unordered_map Class

傳回項目數索引鍵符合參數指定的索引鍵的 hash_map 的。

size_type count(
   const Key& _Key
) const;

參數

  • _Key
    從 hash_map 將相符項目的索引值。

傳回值

如果為, hash_map 包含排序鍵與參數相符索引鍵的項目;,如果 hash_map 不包含具有相符之索引鍵的項目。

備註

成員函式傳回項目數 x 介於

[lower_bound (_Key), upper_bound (_Key))

在 hash_map 情況下,其中就是為,是唯一組合容器。

在 Visual C++ .NET Pocket PC, <hash_map><hash_set> 標頭檔 (Header File) 的成員不在 std 命名空間,,而是移至 stdext 命名空間。 如需詳細資訊,請參閱 stdext 命名空間

範例

當編譯這個範例與 /Wp64 旗標或在一個獨立的平台時,編譯器警告 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

請參閱

參考

hash_map Class

標準樣板程式庫