By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,837 Members | 1,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,837 IT Pros & Developers. It's quick & easy.

how to eliminate duplicates in a multimap?

P: n/a
Hi,

I have a multimap container. I want to eliminate all "duplicate"
elements. By duplicate I mean something like (3, 4), (4, 3) and (4, 3),
in which I want to eliminate any two of these three. The most
straightforward way I can think of is to first swap every member such
that every member has its key smaller than its value. Then loop thru
the multipmap and erase duplicates (please see the loop below). Is
there a more automatic way to achieve this? Thanks for any input.

// swap first
.....

//
for (it=m.begin(); it!=m.end();)
{
IT itUB = m.upper_bound(it->first);
copy(it, itUB, inserter(mAux, mAux.begin()));
it = itUB;
}

m.swap(mAux);

Tony
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
How are the duplicates getting into the map? If they are being placed in
when you search for a key use find() instead of the subscript[] operator.
If this is not possible use a set instead.
On Wed, 13 Apr 2005 21:06:02 +0000, Tony Young wrote:
Hi,

I have a multimap container. I want to eliminate all "duplicate"
elements. By duplicate I mean something like (3, 4), (4, 3) and (4, 3),
in which I want to eliminate any two of these three. The most
straightforward way I can think of is to first swap every member such
that every member has its key smaller than its value. Then loop thru
the multipmap and erase duplicates (please see the loop below). Is
there a more automatic way to achieve this? Thanks for any input.

// swap first
....

//
for (it=m.begin(); it!=m.end();)
{
IT itUB = m.upper_bound(it->first);
copy(it, itUB, inserter(mAux, mAux.begin()));
it = itUB;
}

m.swap(mAux);

Tony


Jul 23 '05 #2

P: n/a
Tony Young wrote:
Hi,

I have a multimap container. I want to eliminate all "duplicate"
elements. By duplicate I mean something like (3, 4), (4, 3) and (4, 3),
in which I want to eliminate any two of these three. The most
straightforward way I can think of is to first swap every member such
that every member has its key smaller than its value. Then loop thru
the multipmap and erase duplicates (please see the loop below). Is
there a more automatic way to achieve this? Thanks for any input.

// swap first
....

//
for (it=m.begin(); it!=m.end();)
{
IT itUB = m.upper_bound(it->first);
copy(it, itUB, inserter(mAux, mAux.begin()));
it = itUB;
}

m.swap(mAux);

Tony


There may be a more clever way to do this, but you could copy all the
elements to a map whose comparison function regards (x,y) = (y,x).
Remember that the comparison fcn is lessThan so you'll need to define
each not less than the other.
Jul 23 '05 #3

P: n/a
On Wed, 13 Apr 2005 21:06:02 GMT, Tony Young <jd*******@yahoo.com>
wrote:
The most
straightforward way I can think of is to first swap every member such
that every member has its key smaller than its value.


It is not allowed -- after inserting pair into map or multimap you
cannot changed its key.

I have no idea how to deal with inverse pairs but assuming that you
want to deal only with straight pairs you don't even have to use any
find method. Just iterate through the multimap -- when previous pair
is equal to current pair erase one of them and continue iterating.
When it is unequal and you erased pair before erase previous one.

Note: after erasing the iterator is not valid.

have a nice day
bye
--
Maciej "MACiAS" Pilichowski http://bantu.fm.interia.pl/

M A R G O T --> http://www.margot.cad.pl/
automatyczny tłumacz (wczesna wersja rozwojowa) angielsko-polski
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.