Multipmap of pointers, sort by key

K

karen.b.lin

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

karen.b.lin

(e-mail address removed) skrev:


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

Obnoxious User

(e-mail address removed) skrev:
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;
 
O

Obnoxious User

Obnoxious User skrev:
(e-mail address removed) skrev:

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

Roland Pibinger

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

karen.b.lin

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



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

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
::_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
::_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
::_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
::_Kfn,struct UnderlyingsPtrSorter,class std:
:allocator<class BOMBPosition *> >::_Lbound(class BOMBImntRT *const
& ) const'
Error executing cl.exe.
 
K

karen.b.lin

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 () ?
 
M

Marco Wahl

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
 
M

Marco Wahl

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

Roland Pibinger

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

karen.b.lin

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;
}
};
 
X

xperthands

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,134
Latest member
Lou6777736
Top