473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Multipmap of pointers, sort by key

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 '07 #1
12 3320
ka*********@gmail.com skrev:
I've created a multimap of pointers. The sort order by key seems
messed up.
How so?

Apr 10 '07 #2
On Apr 10, 1:22 pm, Obnoxious User <O...@127.0.0.1wrote:
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.

Apr 10 '07 #3
ka*********@gmail.com skrev:
On Apr 10, 1:22 pm, Obnoxious User <O...@127.0.0.1wrote:
>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_sortundPortMap;

Apr 10 '07 #4
Obnoxious User skrev:
ka*********@gmail.com skrev:
>On Apr 10, 1:22 pm, Obnoxious User <O...@127.0.0.1wrote:
>>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_sortundPortMap;
--
OU
Apr 10 '07 #5
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_sortundPortMap;

--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Apr 10 '07 #6
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_sortundPortMap;
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 '07 #7
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_sortundPortMap;

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 '07 #8
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_sortundPortMap;
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

Apr 11 '07 #9
On Apr 11, 6:34 pm, "Marco Wahl" <marco.w...@gmail.comwrote:
>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_sortundPortMap;
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.

Apr 11 '07 #10
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
Apr 11 '07 #11
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 '07 #12
On Apr 13, 9:03 am, "karen.b....@gmail.com" <karen.b....@gmail.com>
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<Tand have the key
part
of the type you specify for T.

Apr 13 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tim Partridge | last post by:
How do I define the use of list::sort() when the list holds pointers? For example, #include <list> class foo { public: foo(int i): i(i) {}; ~foo();
14
by: Roland Bengtsson | last post by:
I have a class Conception and I have this in a vector, it should be: vector<Conception> vek; // vector vector<Conception>::iterator vek; // iterator to vek But what if I want to have pointers...
6
by: Der Andere | last post by:
I have an array of pointers (to a class) which I want to have sorted. I have implemented the < operator for the class but I guess STL sort will sort the pointers according to _their_ values (the...
6
by: Matthias | last post by:
Hi, say I have a vector v1: std::vector<SomeType> v1; and I need a vector v2 of pointers to v1's elements: std::vector<SomeType*> v2;
18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
9
by: Timothee Groleau | last post by:
Hi all, My name is Tim, I'm just getting started with C++ and this is my first post to the group. Is there a standard recommended approach to use a vector of pointers along with <algorithm>?...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
22
by: sandy | last post by:
I am trying to make a simulated directory structure application for a course. I am using a Vector to store pointers to my Directory objects (as subdirectories of the current object). In my...
11
by: Jeff Schwab | last post by:
Would std::sort ever compare an object with itself? I'm not talking about two distinct, equal-valued objects, but rather this == &that. The container being sorted is a std::vector. I've never...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.