Search code examples
c++dictionarycontainerskey-valuemultimap

What's the difference between std::multimap<key, value> and std::map<key, std::set<value> >


I found that they have one key and multiple values which is unique.


Solution

  • The multimap stores pairs of (key, value) where both key and value can appear several times.

    The map<key, set<value>> will only store each value once for a specific key. To do that, it will have to be able to compare the values, not just the keys.

    It depends on your application if the values that compare equal are equivalent, or if you wish to store them separately anyway. Perhaps they contain fields that are different but do not take part in the comparison for the set.