Use cases of std::multimap?

A counter-example seems to be in order. Consider a PhoneEntry in an AdressList grouped by name. Int AdressListCompare(const PhoneEntry& p1, const PhoneEntry& p2){ return p1.Name adressList; adressList.

Insert( PhoneEntry("Cpt. G", "123-456", "Cellular") ); adressList. Insert( PhoneEntry("Cpt.

G", "234-567", "Work") ); // Getting the entries addressList. Equal_range( PhoneENtry("Cpt. G") ); // All numbers This would not be feasible with a set+count.

Your Object+count approach seems to be faster if this behavior is not required. For instance the multiset::count() member states "Complexity: logarithmic in size + linear in count.

1 Good example of using custom predicates to put STL containers to good use. – Kerrek SB Jun 24 '11 at 0:35 This would seem to be quite identical to map>. Whilst admittedly not identical to my proposed replacement, it can also be de-composed.

– DeadMG Jun 26 '11 at 20:10 @DeadMG True, but as Alan points out, the iterators behaves very differently for a multiset than the vector combo. Also AdressListCompare doesn't necessarily need to compare only one member. – Captain Giraffe Jun 26 '11 at 21:42 If it were stored as a map> you'd only have that data stored once.In your example, the multiset stores the key repeatedly, because it's part of the value.

– DeadMG Jun 27 '11 at 15:25 @DeadMG Good point. I think we have managed to show the important distinctions between the two approaches; at the very least, that it is useful to consider both approaches when dealing with a dataset like this. – Captain Giraffe Jun 27 '11 at 19:47.

You could use make the substitutions that you suggest, and extract similar behavior. But the interfaces would be very different than when dealing with regular standard containers. A major design theme of these containers is that they share as much interface as possible, making them as interchangeable as possible so that the appropriate container can be chosen without having to change the code that uses it.

For instance, std::map> would have iterators that dereference to std::pair> instead of std::pair. Std::map>::Count() wouldn't return the correct result, failing to account for the duplicates in the vector. Of course you could change your code to do the extra steps needed to correct for this, but now you are interfacing with the container in a much different way.

You can't later drop in unordered_map or some other map implementation to see it performs better. In a broader sense, you are breaking the container abstraction by handling container implementation details in your code rather than having a container that handles it's own business. It's entirely possible that your compiler's implementation of std::multimap is really just a wrapper around std::map>.

Or it might not be. It could be more efficient and friendly to object pool allocation (which vectors are not). Using std::map instead of std::multiset is the same case.Count() would not return the expected value, iterators will not iterate over the duplicates, iterators will dereference to std::pair instead of directly to `K.

A multimap or multiset allows you to have elements with duplicate keys. Ie a set is a non-ordered group of elements that are all unique in that {A,B,C} == {B,C,A}.

1 This completely doesn't answer the question. – DeadMG Jun 23 '11 at 16:17.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions