次の方法で共有


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 Class

set::count (STL Samples)

標準テンプレート ライブラリ