I’m using an stl unordered_map, and I can’t seem to get the count method to work.
This is my program:
typedef unordered_map<char, int> Mymap;
int main()
{
Mymap m;
m.insert(Mymap::value_type('a', 1));
m.insert(Mymap::value_type('b', 2));
m.insert(Mymap::value_type('c', 3));
m.insert(Mymap::value_type('b', 4));
m.insert(Mymap::value_type('b', 5));
cout << m.count('b') << endl;
return 0;
}
The documentation for unordered_map says that unordered_map::count(const Key& k) returns the number of elements with the key k.
So I would expect the output here to be 3, whereas the real output is 1. Why?
An
unordered_mapmaintains a 1:1 mapping of key to value, socountwill always return zero or one.You need an
unordered_multimapif you want to map multiple values to a single key.