I have read the reference on how a multimap can have multiple elements have the same key, but I don't understand how that is even possible. How would it know which element I'm referring to?
Is it like this?
1 2 3 4
std::multimap<char, int> myMap;
myMap["Key"] = 1;
myMap["Key"] = 2;
myMap["Key"] = 3; // Now all three of these are in here seperately?
typedef std::multimap<std::string, int> M;
M myMap;
// multimap has no operator[]. Use the insert function to add elements to the map.
myMap.insert(std::make_pair("Key", 1));
myMap.insert(std::make_pair("Key", 2));
myMap.insert(std::make_pair("Key", 3));
// To get a single element with a specific key you can use the find
// function that returns an iterator to one of the elements with that
// key. Here we use equal_range instead because that returns a pair
// with start and end iterators useful for iterating over all the
// elements with the same key.
std::pair<M::iterator, M::iterator> range = myMap.equal_range("Key");
for (M::iterator it = range.first; it != range.second; ++it)
{
std::cout << it->second << std::endl;
}