how to eliminate duplicates in a multimap?

Discussion in 'C++' started by Tony Young, Apr 13, 2005.

  1. Tony Young

    Tony Young Guest

    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
     
    Tony Young, Apr 13, 2005
    #1
    1. Advertising

  2. Tony Young

    Reddsci2001 Guest

    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
     
    Reddsci2001, Apr 14, 2005
    #2
    1. Advertising

  3. Tony Young

    Mark P Guest

    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.
     
    Mark P, Apr 14, 2005
    #3
  4. On Wed, 13 Apr 2005 21:06:02 GMT, Tony Young <>
    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
     
    Maciej Pilichowski, Apr 14, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Håvard Kverneland

    Multimap

    Håvard Kverneland, Feb 10, 2004, in forum: Java
    Replies:
    1
    Views:
    676
    Håvard Kverneland
    Feb 10, 2004
  2. Replies:
    2
    Views:
    4,235
    Mukul Gandhi
    Jul 20, 2005
  3. John Harrison
    Replies:
    1
    Views:
    601
    Dave O'Hearn
    Aug 14, 2003
  4. Tanguy Fautré

    std::multimap insertion order guarantees

    Tanguy Fautré, Oct 5, 2003, in forum: C++
    Replies:
    13
    Views:
    848
    David B. Held
    Oct 6, 2003
  5. al

    map and multimap

    al, Jan 2, 2004, in forum: C++
    Replies:
    5
    Views:
    489
    Nick Hounsome
    Jan 3, 2004
Loading...

Share This Page