“multiset” & “multimap” - What's the point?

后端 未结 7 1656
孤城傲影
孤城傲影 2020-12-08 18:58

As the question states ... I don\'t get the point about multisets / multimaps.

So, what\'s the purpose?

相关标签:
7条回答
  • 2020-12-08 19:28

    Here's what Wikipedia says about uses:

    • In a student enrollment system, where students may be enrolled in multiple classes simultaneously, there might be an association for each enrollment of a student in a course, where the key is the student ID and the value is the course ID. If a student is enrolled in three courses, there will be three associations containing the same key.
    • The index of a book may report any number of references for a given index term, and thus may be coded as a multimap from index terms to any number of reference locations
    0 讨论(0)
  • 2020-12-08 19:28

    Use multimap, wherever you want to use tree kind of a structure.

    0 讨论(0)
  • 2020-12-08 19:29

    The most important benefit of using a multiset over a vector/list(or any other container) is the time complexity of find operation. average case time complexity for multiset is O(logn) and unordered_multiset is O(1). Same is true for multimap and ordered_multimap.

    0 讨论(0)
  • 2020-12-08 19:30

    http://www.cplusplus.com/reference/stl/multimap/

    Maps are a kind of associative containers that stores elements formed by the combination of a key value and a mapped value, much like map containers, but allowing different elements to have the same key value.

    It is kind of registry where elements can share a key. You can think of companies and employees. Street address is a key and employees are values.

    0 讨论(0)
  • 2020-12-08 19:33

    One example where a multimap would be useful if you had a situation where most of the time the keys are unique, but sometimes they aren't.

    For example, if you were creating a cache class that used a hash as a key. Most of the time two different objects will not have the same hash, so the keys will be unique. But it is possible that you will get hash collisions for different objects, so you would want a multimap to cover that situation.

    Another example would be any sort of non-unique index (like in a database).

    As for a multiset - I think those would be less useful. Only thing I can think of would be to use it as a kind of automatically sorted list.

    0 讨论(0)
  • 2020-12-08 19:36

    A multiset or multimap is simply for situations where there might be more than one of a particular item. For example, let's say you wanted to create an index for a book. You'd scan through the text, throw out all the really common meaningless words ("a", "an", "the", etc.) and then make a list of all the rest, and the place in the book where each occurred.

    Quite a few of the words will appear on more than one page, in which case you'll have multiple entries mapping from one word to different pages. One way to handle that would be a multimap from words to page numbers.

    0 讨论(0)
提交回复
热议问题