Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"multiset" & "multimap" - What's the point?

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

So, what's the purpose?

like image 224
Sebastian Dressler Avatar asked May 18 '10 14:05

Sebastian Dressler


People also ask

What is multiset used for?

The multiset object uses this expression to determine both the order the elements follow in the container and whether two element keys are equivalent (by comparing them reflexively: they are equivalent if ! comp(a,b) && ! comp(b,a)). This can be a function pointer or a function object (see constructor for an example).

What is a C++ multiset?

Multisets are part of the C++ STL (Standard Template Library). Multisets are the associative containers like Set that stores sorted values (the value is itself the key, of type T), but unlike Set which store only unique keys, multiset can have duplicate keys. By default it uses < operator to compare the keys.

What is a multiset in programming?

A MultiSet is a data structure which stores and manipulates an unordered collection of elements which may be repeated. It is implemented as a Maple object.

How do I get multiset in C++?

The multiset::find() is a built-in function in C++ STL which returns an iterator pointing to the lower_bound of the element which is searched in the multiset container. If the element is not found, then the iterator points to the position past the last element in the set.


2 Answers

Some use cases:

multimap

  • With ZIP code as a key, all people which have that ZIP code
  • With account ID as key, all open orders of that person/account
  • A dictionary, with per keyword various explanations

multiset

is in essence a map with a key and a integer count.

  • The inventory of a shop, all products have their key and the amount still available is the value
  • accumulated sales data of a shop, every time a product is sold the product id get's added to the multiset thereby increasing the amount sold
like image 125
extraneon Avatar answered Oct 05 '22 15:10

extraneon


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.

like image 21
Mohammed Habib Avatar answered Oct 05 '22 15:10

Mohammed Habib