Tradeoffs between multi and non-multi associative containers

D

Dave

Hello NG,

Why would one not just always use std::multiset<> instead of std::set<> (and
similarly for maps)? Clearly, there must be tradeoffs to gain the ability
to have repeated keys. What are the tradeoffs?

Thanks,
Dave
 
C

Christopher Benson-Manica

Dave said:
Why would one not just always use std::multiset<> instead of std::set<> (and
similarly for maps)? Clearly, there must be tradeoffs to gain the ability
to have repeated keys. What are the tradeoffs?

I think the specific tradeoffs are related to QOI, whatever they might
be. There may well be times when you specifically do NOT want the
semantics of a std::multiset, I imagine.
 
A

Andrey Tarasevich

Dave said:
...
Why would one not just always use std::multiset<> instead of std::set<> (and
similarly for maps)? Clearly, there must be tradeoffs to gain the ability
to have repeated keys. What are the tradeoffs?
...

In certain applications one might specifically require the functionality
of 'std::set', not 'std::multiset'. For example, one might want to use a
simple combination of 'std::copy', 'std::inserter' and 'std::set' in
order to generate a set of unique elements in a given sequence (say,
'std::vector'). 'std::mutliset' isn't immediately applicable in this case.
 
K

Kevin Goodsell

Dave said:
Hello NG,

Why would one not just always use std::multiset<> instead of std::set<> (and
similarly for maps)? Clearly, there must be tradeoffs to gain the ability
to have repeated keys. What are the tradeoffs?

I don't know for sure, but I definitely use the non-multi versions more
frequently. I think the code would be more complicated using multimaps
when maps do the job. For example, you don't get operator[].

Consider looking up a single element. In a map, it's either found or
not. In a multimap, if it's found, you still might need to check whether
there are others.

Consider storing a unique key-value pair. In a map you just do it. In a
multimap, you have to first look up and remove any existing matching keys.

-Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top