90

I don't understand why multimap exists if we can create map of vectors or map of sets. For me only differences are:

  • using equal_range in multimap for getting elements of a key and in map of vectors we simply use [] operator and have vector of elements.
  • using multimap.insert(make_pair(key,value)) in multimap for adding elements and map_of_vectors[key].push_back(value) in map of vectors.

So why use multimap? For me it's better to have a vector than two iterators to get all values of a key.

This question applies also to unordered_map of vectors and unordered_multimap.

Diego Sevilla
  • 28,636
  • 4
  • 59
  • 87
Mariusz Pawelski
  • 25,983
  • 11
  • 67
  • 80
  • 14
    I must admit I never quite understood the purpose of `multimap` :/ – Matthieu M. Dec 14 '10 at 12:13
  • I little bit late in the question but also multimap consumes much more memory than map of vectors due the extra pointers. The only reason I'd use them is if I want to keep the key of each element (doing `push_back` you won't keep it) – Jcao02 Aug 05 '14 at 14:08
  • Multimap is great if you not only want to keep track of duplicate keys of differing values, but you also want to delete any key/value pair at a moments notice. A map of vectors isn't suitable for that, and while you can use a map of lists, it's more convenient to just use a multimap. – richizy Jul 11 '16 at 17:27
  • 1
    While I see your point (and I also don't tend to use multimap), about the same thing can be said about `map` vs. `set, cmp_first>` where cmp_first compares the `.first` member. – lorro Aug 26 '16 at 23:43
  • 1
    map of sets: http://stackoverflow.com/questions/8602068/whats-the-difference-between-stdmultimapkey-value-and-stdmapkey-stds – Ciro Santilli OurBigBook.com Dec 31 '16 at 22:12
  • 1
    @CiroSantilli新疆再教育营六四事件法轮功郝海东 what about 'em? – luizfls Nov 01 '21 at 20:45

2 Answers2

68

I would say it depends on whether all the values with same key have a relationship that you want to address.

So for example, do you often go through all elements with key X, or pass them to a function, and so on? Then it is more convenient to already have them in their separate container, that you can address directly.

However, if you just have a collection of items, that may share same key value, or not, why use vectors in between? It is more convenient to run through the multimap with iterators than have a nested for-loop for the map, vector case.

Another way of looking at this: If multiple entries per key is very common, your structure is more efficient in the map, vector case. If they seldomly happen, it is the opposite.

ypnos
  • 50,202
  • 14
  • 95
  • 141
  • 3
    Thanks. Your and Artyom's answer showed me a little more differences. However I still don't believe multimap is as useful in real life as map of vectors. But that's my personal opinion ;) – Mariusz Pawelski Dec 15 '10 at 18:36
  • My practical extract from this answer is: with `multimap` you can have iterator range covering the whole structure but such begin/end iterators will iterate pairs of key and value (which may not be convenient). With `map<...,vector>` iterating the whole structure is more complex, but you can have begin/end iterators of equal sequence of just values. – Jarek C Oct 07 '19 at 07:10
65

There are many important differences between multimap<x, y> and map<x, vector<y>>

Once you had inserted a value into multimap, you know that the iterator would remain valid until you remove it and this is very strong property, you can't have it with map of vectors.

multimap<x,y>::iterator p=mymap.insert(make_pair(a,b));

The iterator remains valid until it is erased from map, while in second case, it would be invalidated each time you add new entry to the vector.

Also note that map<x, vector<y>> may have an empty value set with existing key, while multimap does not.

These are different things that behave differently.

And to be honest I miss multimap in some languages that do not provide it in their library.

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
Artyom
  • 31,019
  • 21
  • 127
  • 215
  • 3
    While the angle of iterator-invalidation is interesting, your statement *"it would be invalidated **each** time you add new entry to the vector."* is not practically true. The iterator gets invalidated only when the vector resizes, else not... though since you don't usually know/care when it gets resized, we just shouldn't design our code around the resizing and simply *assume* iterators get invalidated on insertion! – Nawaz Jul 30 '18 at 18:42
  • @Nawaz consider inserting (adding new) and erasing – Gelldur Jul 23 '19 at 12:00