Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Multipmap of pointers, sort by key

Reply
Thread Tools

Multipmap of pointers, sort by key

 
 
karen.b.lin@gmail.com
Guest
Posts: n/a
 
      04-10-2007
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~

 
Reply With Quote
 
 
 
 
Obnoxious User
Guest
Posts: n/a
 
      04-10-2007
skrev:
> I've created a multimap of pointers. The sort order by key seems
> messed up.
>


How so?

 
Reply With Quote
 
 
 
 
karen.b.lin@gmail.com
Guest
Posts: n/a
 
      04-10-2007
On Apr 10, 1:22 pm, Obnoxious User <O...@127.0.0.1> wrote:
> karen.b....@gmail.com 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.





 
Reply With Quote
 
Obnoxious User
Guest
Posts: n/a
 
      04-10-2007
skrev:
> On Apr 10, 1:22 pm, Obnoxious User <O...@127.0.0.1> wrote:
>> karen.b....@gmail.com 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;

 
Reply With Quote
 
Obnoxious User
Guest
Posts: n/a
 
      04-10-2007
Obnoxious User skrev:
> skrev:
>> On Apr 10, 1:22 pm, Obnoxious User <O...@127.0.0.1> wrote:
>>> karen.b....@gmail.com 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
 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      04-10-2007
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
 
Reply With Quote
 
karen.b.lin@gmail.com
Guest
Posts: n/a
 
      04-11-2007
On Apr 10, 2:50 pm, rpbg...@yahoo.com (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(51 : 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:air<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:air<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(51 : 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:air<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:air<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.


 
Reply With Quote
 
karen.b.lin@gmail.com
Guest
Posts: n/a
 
      04-11-2007
On Apr 11, 10:27 am, "karen.b....@gmail.com" <karen.b....@gmail.com>
wrote:
> On Apr 10, 2:50 pm, rpbg...@yahoo.com (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(51 : 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:air<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:air<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(51 : 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:air<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:air<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 () ?

 
Reply With Quote
 
Marco Wahl
Guest
Posts: n/a
 
      04-11-2007

> > > >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

 
Reply With Quote
 
Marco Wahl
Guest
Posts: n/a
 
      04-11-2007
On Apr 11, 6:34 pm, "Marco Wahl" <marco.w...@gmail.com> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
another way to sort like l.sort(key=lambda x:(x[0][0], -x[1][0])) sajuptpm Python 7 09-08-2010 06:55 PM
Using s.sort([cmp[, key[, reverse]]]) to sort a list of objects based on a attribute cjt22@bath.ac.uk Python 7 09-10-2007 11:10 AM
xsl:sort using an xsl:variable as the sort key jobooker@gmail.com XML 2 09-05-2006 03:51 PM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM
sort multi-key hash by value and print out with key value pairs Antonio Quinonez Perl Misc 2 08-14-2003 10:56 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57