共用方式為


set::count

傳回項目數索引鍵符合參數所指定之索引鍵的集合。

size_type count( 
   const Key& _Key 
) const;

參數

  • _Key
    要從集合中符合之項目的索引鍵。

傳回值

1,如果這個集合包含排序鍵與參數相符索引鍵的項目。0,如果集合未包含具有相符索引鍵的項目。

備註

成員函式傳回項目數位在下列範圍的:

[lower_bound (_Key), upper_bound (_Key))。

範例

當編譯這個範例會使用 /Wp64 旗標或在 64 位元平台時,編譯器警告 C4267 會產生。 如需這項警告的詳細資訊,請參閱 編譯器警告 (層級 3) C4267

// set_count.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main()
{
    using namespace std;
    set<int> s1;
    set<int>::size_type i;

    s1.insert(1);
    s1.insert(1);

    // Keys must be unique in set, so duplicates are ignored
    i = s1.count(1);
    cout << "The number of elements in s1 with a sort key of 1 is: "
         << i << "." << endl;

    i = s1.count(2);
    cout << "The number of elements in s1 with a sort key of 2 is: "
         << i << "." << endl;
}
  

需求

標頭: <set>

命名空間: std

請參閱

參考

set 類別

set::count (STL 範例)

標準樣板程式庫