map::count
Visual Studio 2010
Returns the number of elements in a map whose key matches a parameter-specified key.
size_type count( const Key& _Key ) const;
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.
// 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;
}
The number of elements in m1 with a sort key of 1 is: 1. The number of elements in m1 with a sort key of 2 is: 1. The number of elements in m1 with a sort key of 3 is: 0.