Using a multimap
From: Dennis Jones (nospam_at_bogus.com)
Date: Mon, 16 Aug 2004 08:51:07 -0700
Is there a way to iterate through a multimap in such a way as to encounter
only the unique keys? In other words, since a multimap allows duplicate
keys, I would like to iterate through the entire multimap, and if a
particular key has duplicates, only see one of them. I don't think I care
about which of the duplicate entries I see, because once I have an entry, I
can use lower_bound and upper_bound to iterate through them, right? (I
won't know the keys ahead of time, so I cannot use multimap::find)
Maybe a multimap isn't even the right way to solve my problem. Here is what
I need to do: I may have some number of items (structures) in which some of
the items may contain the same value in one of its data members. For
There may be any number of MyStruct instances, and several may have the same
value for 'x'. If this is the case, it identifies a conflict which must be
corrected by the user. So, I need to identify those entries that have
duplicate 'x's so that the user will be aware of the conflicts and can
correct them. (Unfortunately, it is not possible to prevent the conflicts
in the first place -- which is why this problem exists at all.)
My idea was to copy the entries into a multimap using the member 'x' as the
key, and then somehow identify the entries in the multimap that have
duplicate keys. However, I am not sure how best to go about doing that.
Can anyone point me in the right direction, or suggest an alternative
solution to my problem?
Maybe another solution is to use both a map and a multimap. I could fill
both with the same entries. Since the map will not allow duplicates, any
duplicates that exist will get overritten. Then I could iterate through the
map (obtaining all of the unique keys) and use those keys as the search
criteria in multimap::find. Of course, if the list of entries is large,
this solution could use a lot of memory overhead.
Any other ideas are welcome.