Multipmap of pointers, sort by key

Discussion in 'C++' started by karen.b.lin@gmail.com, Apr 10, 2007.

  1. Guest

    I've created a multimap of pointers. The sort order by key seems
    messed up.

    ex:

    std::multimap<BOMBImntRT*, BOMBPortfolio*> undPortMap;

    I did not try to create multimap of objects instead, b/c some classes
    do not have copy constructors, and it'll be very messy to create one.

    Is there anyway to have the pointer multimap sorted?

    New at this ... appreciate your help~
    , Apr 10, 2007
    #1
    1. Advertising

  2. skrev:
    > I've created a multimap of pointers. The sort order by key seems
    > messed up.
    >


    How so?
    Obnoxious User, Apr 10, 2007
    #2
    1. Advertising

  3. Guest

    On Apr 10, 1:22 pm, Obnoxious User <O...@127.0.0.1> wrote:
    > skrev:
    >
    > > I've created a multimap of pointers. The sort order by key seems
    > > messed up.

    >
    > How so?


    the key is an instrument, operator < is on the name of the instrument.

    Here's an example of the map:

    Instrument Portfolio
    AMD P1
    MOTOROLA P1
    PACIFIC ETHANOL P1
    MERRILL LYNCH P2
    MOTOROLA P2


    Even tho and line 2 and line 5, both keys are motorola, when i print
    them out, it's not sorted by the Instrument.
    The order seems to be the insert order.
    , Apr 10, 2007
    #3
  4. skrev:
    > On Apr 10, 1:22 pm, Obnoxious User <O...@127.0.0.1> wrote:
    >> skrev:
    >>
    >>> I've created a multimap of pointers. The sort order by key seems
    >>> messed up.

    >> How so?

    >
    > the key is an instrument, operator < is on the name of the instrument.
    >
    > Here's an example of the map:
    >
    > Instrument Portfolio
    > AMD P1
    > MOTOROLA P1
    > PACIFIC ETHANOL P1
    > MERRILL LYNCH P2
    > MOTOROLA P2
    >
    >
    > Even tho and line 2 and line 5, both keys are motorola, when i print
    > them out, it's not sorted by the Instrument.
    > The order seems to be the insert order.


    With pointers as keys, it will by default sort using pointer addresses.
    Add your own sorting functor to sort it the way you want.

    struct my_sort {
    bool operator()(BOMBImntRT * a, BOMBImntRT * b) {
    return a->some_value < b->some_value;
    }
    };

    std::multimap<BOMBImntRT*, BOMBPortfolio*, my_sort> undPortMap;
    Obnoxious User, Apr 10, 2007
    #4
  5. Obnoxious User skrev:
    > skrev:
    >> On Apr 10, 1:22 pm, Obnoxious User <O...@127.0.0.1> wrote:
    >>> skrev:
    >>>
    >>>> I've created a multimap of pointers. The sort order by key seems
    >>>> messed up.
    >>> How so?

    >>
    >> the key is an instrument, operator < is on the name of the instrument.
    >>
    >> Here's an example of the map:
    >>
    >> Instrument Portfolio
    >> AMD P1
    >> MOTOROLA P1
    >> PACIFIC ETHANOL P1
    >> MERRILL LYNCH P2
    >> MOTOROLA P2
    >>
    >>
    >> Even tho and line 2 and line 5, both keys are motorola, when i print
    >> them out, it's not sorted by the Instrument.
    >> The order seems to be the insert order.

    >
    > With pointers as keys, it will by default sort using pointer addresses.
    > Add your own sorting functor to sort it the way you want.
    >
    > struct my_sort {
    > bool operator()(BOMBImntRT * a, BOMBImntRT * b) {
    > return a->some_value < b->some_value;


    Or if you already have operator< defined

    return *a < *b;

    > }
    > };
    >
    > std::multimap<BOMBImntRT*, BOMBPortfolio*, my_sort> undPortMap;
    >


    --
    OU
    Obnoxious User, Apr 10, 2007
    #5
  6. On Tue, 10 Apr 2007 19:47:25 +0200, Obnoxious User wrote:
    >With pointers as keys, it will by default sort using pointer addresses.


    .... because STL was designed only for values. For pointers you need
    workarounds.

    >Add your own sorting functor to sort it the way you want.
    >
    >struct my_sort {
    > bool operator()(BOMBImntRT * a, BOMBImntRT * b) {


    bool operator()(const BOMBImntRT * a, const BOMBImntRT * b) const {

    > return a->some_value < b->some_value;
    > }
    >};
    >
    >std::multimap<BOMBImntRT*, BOMBPortfolio*, my_sort> undPortMap;





    --
    Roland Pibinger
    "The best software is simple, elegant, and full of drama" - Grady Booch
    Roland Pibinger, Apr 10, 2007
    #6
  7. Guest

    On Apr 10, 2:50 pm, (Roland Pibinger) wrote:
    > On Tue, 10 Apr 2007 19:47:25 +0200, Obnoxious User wrote:
    > >With pointers as keys, it will by default sort using pointer addresses.

    >
    > ... because STL was designed only for values. For pointers you need
    > workarounds.
    >
    > >Add your own sorting functor to sort it the way you want.

    >
    > >struct my_sort {
    > > bool operator()(BOMBImntRT * a, BOMBImntRT * b) {

    >
    > bool operator()(const BOMBImntRT * a, const BOMBImntRT * b) const {
    >
    > > return a->some_value < b->some_value;
    > > }
    > >};

    >
    > >std::multimap<BOMBImntRT*, BOMBPortfolio*, my_sort> undPortMap;


    Thanks for the reply

    A stupid question ... why are we overloading opeartor ()?

    struct UnderlyingsPtrSorter
    {
    bool operator() (BOMBImntRT* a, BOMBImntRT* b){
    return a->ImntName < b->ImntName;
    }
    };


    std::multimap<BOMBImntRT*, BOMBPortfolio*, UnderlyingsPtrSorter>
    undPortMap;
    std::multimap<BOMBImntRT*, BOMBPosition*, UnderlyingsPtrSorter>
    undPosMap;

    std::multimap<BOMBImntRT*, BOMBPortfolio*,
    UnderlyingsPtrSorter> ::iterator undPortIt;
    std::multimap<BOMBImntRT*, BOMBPosition*,
    UnderlyingsPtrSorter> ::iterator undPosIt;



    I'm getting these compiling errors:

    c:\program files\microsoft visual studio enterprise edition
    \vc98\include\xtree(518) : error C2662: '()' : cannot convert 'this'
    pointer from 'const struct UnderlyingsPtrSorter' to 'struct
    UnderlyingsPtrSorter &'
    Conversion loses qualifiers
    c:\program files\microsoft visual studio enterprise edition
    \vc98\include\xtree(514) : while compiling class-template member
    function 'struct std::_Tree<class BOMBImntRT *,struct std::pair<class
    BOMBImntRT * const,class BOMBPosition *>,struct
    std::multimap<class BOMBImntRT *,class BOMBPosition *,struct
    UnderlyingsPtrSorter,class std::allocator<class BOMBPosition *>
    >::_Kfn,struct UnderlyingsPtrSorter,class std::allocator<class

    BOMBPosition *> >::_Node *__thiscall std::_Tree<class BOMBIm
    ntRT *,struct std::pair<class BOMBImntRT * const,class BOMBPosition
    *>,struct std::multimap<class BOMBImntRT *,class BOMBPosition *,struct
    UnderlyingsPtrSorter,class std::allocator<class BOMBPosition *>
    >::_Kfn,struct UnderlyingsPtrSorter,class std:

    :allocator<class BOMBPosition *> >::_Lbound(class BOMBImntRT *const
    & ) const'
    c:\program files\microsoft visual studio enterprise edition
    \vc98\include\xtree(518) : error C2064: term does not evaluate to a
    function
    c:\program files\microsoft visual studio enterprise edition
    \vc98\include\xtree(514) : while compiling class-template member
    function 'struct std::_Tree<class BOMBImntRT *,struct std::pair<class
    BOMBImntRT * const,class BOMBPosition *>,struct
    std::multimap<class BOMBImntRT *,class BOMBPosition *,struct
    UnderlyingsPtrSorter,class std::allocator<class BOMBPosition *>
    >::_Kfn,struct UnderlyingsPtrSorter,class std::allocator<class

    BOMBPosition *> >::_Node *__thiscall std::_Tree<class BOMBIm
    ntRT *,struct std::pair<class BOMBImntRT * const,class BOMBPosition
    *>,struct std::multimap<class BOMBImntRT *,class BOMBPosition *,struct
    UnderlyingsPtrSorter,class std::allocator<class BOMBPosition *>
    >::_Kfn,struct UnderlyingsPtrSorter,class std:

    :allocator<class BOMBPosition *> >::_Lbound(class BOMBImntRT *const
    & ) const'
    Error executing cl.exe.
    , Apr 11, 2007
    #7
  8. Guest

    On Apr 11, 10:27 am, "" <>
    wrote:
    > On Apr 10, 2:50 pm, (Roland Pibinger) wrote:
    >
    >
    >
    >
    >
    > > On Tue, 10 Apr 2007 19:47:25 +0200, Obnoxious User wrote:
    > > >With pointers as keys, it will by default sort using pointer addresses.

    >
    > > ... because STL was designed only for values. For pointers you need
    > > workarounds.

    >
    > > >Add your own sorting functor to sort it the way you want.

    >
    > > >struct my_sort {
    > > > bool operator()(BOMBImntRT * a, BOMBImntRT * b) {

    >
    > > bool operator()(const BOMBImntRT * a, const BOMBImntRT * b) const {

    >
    > > > return a->some_value < b->some_value;
    > > > }
    > > >};

    >
    > > >std::multimap<BOMBImntRT*, BOMBPortfolio*, my_sort> undPortMap;

    >
    > Thanks for the reply
    >
    > A stupid question ... why are we overloading opeartor ()?
    >
    > struct UnderlyingsPtrSorter
    > {
    > bool operator() (BOMBImntRT* a, BOMBImntRT* b){
    > return a->ImntName < b->ImntName;
    > }
    >
    > };
    >
    > std::multimap<BOMBImntRT*, BOMBPortfolio*, UnderlyingsPtrSorter>
    > undPortMap;
    > std::multimap<BOMBImntRT*, BOMBPosition*, UnderlyingsPtrSorter>
    > undPosMap;
    >
    > std::multimap<BOMBImntRT*, BOMBPortfolio*,
    > UnderlyingsPtrSorter> ::iterator undPortIt;
    > std::multimap<BOMBImntRT*, BOMBPosition*,
    > UnderlyingsPtrSorter> ::iterator undPosIt;
    >
    > I'm getting these compiling errors:
    >
    > c:\program files\microsoft visual studio enterprise edition
    > \vc98\include\xtree(518) : error C2662: '()' : cannot convert 'this'
    > pointer from 'const struct UnderlyingsPtrSorter' to 'struct
    > UnderlyingsPtrSorter &'
    > Conversion loses qualifiers
    > c:\program files\microsoft visual studio enterprise edition
    > \vc98\include\xtree(514) : while compiling class-template member
    > function 'struct std::_Tree<class BOMBImntRT *,struct std::pair<class
    > BOMBImntRT * const,class BOMBPosition *>,struct
    > std::multimap<class BOMBImntRT *,class BOMBPosition *,struct
    > UnderlyingsPtrSorter,class std::allocator<class BOMBPosition *>>::_Kfn,struct UnderlyingsPtrSorter,class std::allocator<class
    >
    > BOMBPosition *> >::_Node *__thiscall std::_Tree<class BOMBIm
    > ntRT *,struct std::pair<class BOMBImntRT * const,class BOMBPosition
    > *>,struct std::multimap<class BOMBImntRT *,class BOMBPosition *,struct
    > UnderlyingsPtrSorter,class std::allocator<class BOMBPosition *>>::_Kfn,struct UnderlyingsPtrSorter,class std:
    >
    > :allocator<class BOMBPosition *> >::_Lbound(class BOMBImntRT *const
    > & ) const'
    > c:\program files\microsoft visual studio enterprise edition
    > \vc98\include\xtree(518) : error C2064: term does not evaluate to a
    > function
    > c:\program files\microsoft visual studio enterprise edition
    > \vc98\include\xtree(514) : while compiling class-template member
    > function 'struct std::_Tree<class BOMBImntRT *,struct std::pair<class
    > BOMBImntRT * const,class BOMBPosition *>,struct
    > std::multimap<class BOMBImntRT *,class BOMBPosition *,struct
    > UnderlyingsPtrSorter,class std::allocator<class BOMBPosition *>>::_Kfn,struct UnderlyingsPtrSorter,class std::allocator<class
    >
    > BOMBPosition *> >::_Node *__thiscall std::_Tree<class BOMBIm
    > ntRT *,struct std::pair<class BOMBImntRT * const,class BOMBPosition
    > *>,struct std::multimap<class BOMBImntRT *,class BOMBPosition *,struct
    > UnderlyingsPtrSorter,class std::allocator<class BOMBPosition *>>::_Kfn,struct UnderlyingsPtrSorter,class std:
    >
    > :allocator<class BOMBPosition *> >::_Lbound(class BOMBImntRT *const
    > & ) const'
    > Error executing cl.exe.- Hide quoted text -
    >
    > - Show quoted text -


    Never mind ... It worked after I add the const
    struct UnderlyingsPtrSorter
    {
    bool operator() (const BOMBImntRT* a, const BOMBImntRT* b) const{
    return a->ImntName < b->ImntName;
    }
    };

    But still don't get why overloading () ?
    , Apr 11, 2007
    #8
  9. Marco Wahl Guest


    > > > >With pointers as keys, it will by default sort using pointer addresses.
    > > > ... because STL was designed only for values. For pointers you need
    > > > workarounds.
    > > > >Add your own sorting functor to sort it the way you want.

    >
    > > > >struct my_sort {
    > > > > bool operator()(BOMBImntRT * a, BOMBImntRT * b) {

    >
    > > > bool operator()(const BOMBImntRT * a, const BOMBImntRT * b) const {

    >
    > > > > return a->some_value < b->some_value;
    > > > > }
    > > > >};

    >
    > > > >std::multimap<BOMBImntRT*, BOMBPortfolio*, my_sort> undPortMap;

    >
    > > Thanks for the reply

    >
    > > A stupid question ... why are we overloading opeartor ()?


    > [...]


    > But still don't get why overloading () ?


    The comparision obviously needs two parameters for work. And this is
    realized by a something that looks like a function that accepts two
    parameters. E.g.

    void my_sort(BOMBImntRT* a, BOMBImntRT* b);

    or

    struct my_sort
    {
    void operator(BOMBImntRT*, BOMBImntRT*);
    }

    hth
    Marco Wahl, Apr 11, 2007
    #9
  10. Marco Wahl Guest

    On Apr 11, 6:34 pm, "Marco Wahl" <> wrote:
    > > > > >With pointers as keys, it will by default sort using pointer addresses.
    > > > > ... because STL was designed only for values. For pointers you need
    > > > > workarounds.
    > > > > >Add your own sorting functor to sort it the way you want.

    >
    > > > > >struct my_sort {
    > > > > > bool operator()(BOMBImntRT * a, BOMBImntRT * b) {

    >
    > > > > bool operator()(const BOMBImntRT * a, const BOMBImntRT * b) const {

    >
    > > > > > return a->some_value < b->some_value;
    > > > > > }
    > > > > >};

    >
    > > > > >std::multimap<BOMBImntRT*, BOMBPortfolio*, my_sort> undPortMap;

    >
    > > > Thanks for the reply

    >
    > > > A stupid question ... why are we overloading opeartor ()?

    > > [...]
    > > But still don't get why overloading () ?

    >
    > The comparision obviously needs two parameters for work. And this is
    > realized by a something that looks like a function that accepts two
    > parameters. E.g.
    >
    > void my_sort(BOMBImntRT* a, BOMBImntRT* b);
    >
    > or
    >
    > struct my_sort
    > {
    > void operator(BOMBImntRT*, BOMBImntRT*);


    void operator()(BOMBImntRT*, BOMBImntRT*);

    >
    > }


    Quick fix, sorry.
    Marco Wahl, Apr 11, 2007
    #10
  11. On 11 Apr 2007 07:58:09 -0700, "karen.b.lin@...com" wrote:

    >But still don't get why overloading () ?


    It's just a requirement of the STL. Alternatively the function might
    have been called 'compare' or something like that. The advantage of
    the STL solution is that you can use as predicate a free function and
    an object with operator() alike.


    --
    Roland Pibinger
    "The best software is simple, elegant, and full of drama" - Grady Booch
    Roland Pibinger, Apr 11, 2007
    #11
  12. Guest

    Thank u all for the replies. Just want to further perfect my codes,
    if i want to sort first by the key, then by value. How will i specify
    it in my sorter, something like this? not sure where do i put the
    value type in the sorter?

    key: BOMBImntRT*
    value: BOMBPortfolio*

    struct UnderlyingsPtrSorter
    {
    bool operator() (const BOMBImntRT* a, const BOMBImntRT* b) const{
    if(a->ImntName == b->ImntName)
    //??
    else
    return a->ImntName < b->ImntName;
    }
    };
    , Apr 13, 2007
    #12
  13. xperthands Guest

    On Apr 13, 9:03 am, "" <>
    wrote:
    > Thank u all for the replies. Just want to further perfect my codes,
    > if i want to sort first by the key, then by value. How will i specify
    > it in my sorter, something like this? not sure where do i put the
    > value type in the sorter?


    You don't. Only the key will be passed to the predicate. If you want
    the
    value to be considered you should use std::set<T> and have the key
    part
    of the type you specify for T.
    xperthands, Apr 13, 2007
    #13
    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. Replies:
    2
    Views:
    9,259
    Martin Honnen
    Sep 5, 2006
  2. Replies:
    7
    Views:
    737
    Stefan Arentz
    Sep 10, 2007
  3. sajuptpm
    Replies:
    7
    Views:
    805
    Terry Reedy
    Sep 8, 2010
  4. Navin
    Replies:
    1
    Views:
    684
    Ken Schaefer
    Sep 9, 2003
  5. Antonio Quinonez
    Replies:
    2
    Views:
    164
    Antonio Quinonez
    Aug 14, 2003
Loading...

Share This Page