I'm looking for a .Net implementation of a multiset. Can anyone recommend a good one?
(A multiset, or bag, is a set that can have duplicate values, and on which you can do set operations: intersection, difference, etc. A shopping cart for instance could be thought of as a multiset because you can have multiple occurrences of the same product.)
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).
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.
If you have a structure where you don't need a key/value but you need the search properties of a set with multiple elements per key, you use a multiset.
In case of Set, data is stored in sorted order. In case of MultiSet also the data is stored in sorted order. In Set duplicate values are not allowed to get stored.
I do not know about one, however you could use a Dictionary
for that, in which the value is the quantity of the item. And when the item is added for the second time, you vould increase the value for it in the dictionary.
An other possibility would be to simply use a List
of items, in which you could put duplicates. This might be a better approach for a shopping cart.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With