-
March 17th, 2009, 11:29 PM
#1
Where Set and Multiset is mostly required
Hi,
In the Associative container group we have map, multimap, set and multiset.
I can understand why map and multimap is used. But set and multiset makes me think where these containers are useful. We can do the samething by using vector (pushing only unique elements) and sorting them.
so what is the use of Set and multi set??
Thanks
-
March 17th, 2009, 11:34 PM
#2
Re: Where Set and Multiset is mostly required
There is more than one way to skin a cat
-
March 18th, 2009, 12:01 AM
#3
Re: Where Set and Multiset is mostly required
Basically they have different big-Oh costs for some operations. To keep an often-changing vector sorted all the time, you'd have to do a sort on every insertion----O(n*n*log n) in total. A multiset would only require O(n*log n), since it knows how to insert-in-order cheaply unlike a vector. (Even if you explicitly inserted in order, the vector would still need to shuffle over all the greater elements----O(n*n).)
Also, a set is pretty much the easiest way to select a bunch of unique elements if the total number of possible elements is not small. (If it were, you could use a bool array to decide if you'd accepted a given element yet.)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|