Partager via


hash_multimap::count

Returns the number of elements in a hash_multimap whose key matches a parameter-specified key.

size_type count(
   const Key& _Key
) const;

Parameters

  • _Key
    The key of the elements to be matched from the hash_multimap.

Return Value

1 if the hash_multimap contains an element whose sort key matches the parameter key; 0 if the hash_multimap doesn't contain an element with a matching key.

Remarks

The member function returns the number of elements in the range

[lower_bound (_Key ), upper_bound (_Key ) )

which have a key value _Key.

In Visual C++ .NET 2003, members of the <hash_map> and <hash_set> header files are no longer in the std namespace, but rather have been moved into the stdext namespace. See The stdext Namespace for more information.

Example

When compiling this example with the /Wp64 flag or on a 64-bit platform, compiler warning C4267 will be generated. For more information on this warning, see Compiler Warning (level 3) C4267.

// hash_multimap_count.cpp
// compile with: /EHsc
#define _DEFINE_DEPRECATED_HASH_CLASSES 0
#include <hash_map>
#include <iostream>

int main( )
{
    using namespace std;
    using namespace stdext;
    hash_multimap<int, int> hm1;
    hash_multimap<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));

    // Elements do not need to have unique keys in hash_multimap,
    // so duplicates are allowed and counted
    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;
}

The number of elements in hm1 with a sort key of 1 is: 2.
The number of elements in hm1 with a sort key of 2 is: 2.
The number of elements in hm1 with a sort key of 3 is: 0.

Requirements

Header: <hash_map>

Namespace: stdext

See Also

Concepts

hash_multimap Class

hash_multimap Members

Standard Template Library