473,569 Members | 3,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multipmap of pointers, sort by key

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

ex:

std::multimap<B OMBImntRT*, BOMBPortfolio*u ndPortMap;

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 3335
ka*********@gma il.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.1 wrote:
karen.b....@gma il.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*********@gma il.com skrev:
On Apr 10, 1:22 pm, Obnoxious User <O...@127.0.0.1 wrote:
>karen.b....@gm ail.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()(BOMB ImntRT * a, BOMBImntRT * b) {
return a->some_value < b->some_value;
}
};

std::multimap<B OMBImntRT*, BOMBPortfolio*, my_sortundPortM ap;

Apr 10 '07 #4
Obnoxious User skrev:
ka*********@gma il.com skrev:
>On Apr 10, 1:22 pm, Obnoxious User <O...@127.0.0.1 wrote:
>>karen.b....@g mail.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()(BOMB ImntRT * a, BOMBImntRT * b) {
return a->some_value < b->some_value;
Or if you already have operator< defined

return *a < *b;
}
};

std::multimap<B OMBImntRT*, BOMBPortfolio*, my_sortundPortM ap;
--
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()(BOMB ImntRT * a, BOMBImntRT * b) {
bool operator()(cons t BOMBImntRT * a, const BOMBImntRT * b) const {
return a->some_value < b->some_value;
}
};

std::multimap< BOMBImntRT*, BOMBPortfolio*, my_sortundPortM ap;

--
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.c om (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()(BOMB ImntRT * a, BOMBImntRT * b) {

bool operator()(cons t BOMBImntRT * a, const BOMBImntRT * b) const {
return a->some_value < b->some_value;
}
};
std::multimap<B OMBImntRT*, BOMBPortfolio*, my_sortundPortM ap;
Thanks for the reply

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

struct UnderlyingsPtrS orter
{
bool operator() (BOMBImntRT* a, BOMBImntRT* b){
return a->ImntName < b->ImntName;
}
};
std::multimap<B OMBImntRT*, BOMBPortfolio*, UnderlyingsPtrS orter>
undPortMap;
std::multimap<B OMBImntRT*, BOMBPosition*, UnderlyingsPtrS orter>
undPosMap;

std::multimap<B OMBImntRT*, BOMBPortfolio*,
UnderlyingsPtrS orter::iterator undPortIt;
std::multimap<B OMBImntRT*, BOMBPosition*,
UnderlyingsPtrS orter::iterator undPosIt;

I'm getting these compiling errors:

c:\program files\microsoft visual studio enterprise edition
\vc98\include\x tree(518) : error C2662: '()' : cannot convert 'this'
pointer from 'const struct UnderlyingsPtrS orter' to 'struct
UnderlyingsPtrS orter &'
Conversion loses qualifiers
c:\program files\microsoft visual studio enterprise edition
\vc98\include\x tree(514) : while compiling class-template member
function 'struct std::_Tree<clas s BOMBImntRT *,struct std::pair<class
BOMBImntRT * const,class BOMBPosition *>,struct
std::multimap<c lass BOMBImntRT *,class BOMBPosition *,struct
UnderlyingsPtrS orter,class std::allocator< class BOMBPosition *>
>::_Kfn,struc t UnderlyingsPtrS orter,class std::allocator< class
BOMBPosition *::_Node *__thiscall std::_Tree<clas s BOMBIm
ntRT *,struct std::pair<class BOMBImntRT * const,class BOMBPosition
*>,struct std::multimap<c lass BOMBImntRT *,class BOMBPosition *,struct
UnderlyingsPtrS orter,class std::allocator< class BOMBPosition *>
>::_Kfn,struc t UnderlyingsPtrS orter,class std:
:allocator<clas s BOMBPosition *::_Lbound(clas s BOMBImntRT *const
& ) const'
c:\program files\microsoft visual studio enterprise edition
\vc98\include\x tree(518) : error C2064: term does not evaluate to a
function
c:\program files\microsoft visual studio enterprise edition
\vc98\include\x tree(514) : while compiling class-template member
function 'struct std::_Tree<clas s BOMBImntRT *,struct std::pair<class
BOMBImntRT * const,class BOMBPosition *>,struct
std::multimap<c lass BOMBImntRT *,class BOMBPosition *,struct
UnderlyingsPtrS orter,class std::allocator< class BOMBPosition *>
>::_Kfn,struc t UnderlyingsPtrS orter,class std::allocator< class
BOMBPosition *::_Node *__thiscall std::_Tree<clas s BOMBIm
ntRT *,struct std::pair<class BOMBImntRT * const,class BOMBPosition
*>,struct std::multimap<c lass BOMBImntRT *,class BOMBPosition *,struct
UnderlyingsPtrS orter,class std::allocator< class BOMBPosition *>
>::_Kfn,struc t UnderlyingsPtrS orter,class std:
:allocator<clas s BOMBPosition *::_Lbound(clas s BOMBImntRT *const
& ) const'
Error executing cl.exe.
Apr 11 '07 #7
On Apr 11, 10:27 am, "karen.b....@gm ail.com" <karen.b....@gm ail.com>
wrote:
On Apr 10, 2:50 pm, rpbg...@yahoo.c om (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()(BOMB ImntRT * a, BOMBImntRT * b) {
bool operator()(cons t BOMBImntRT * a, const BOMBImntRT * b) const {
return a->some_value < b->some_value;
}
>};
>std::multimap< BOMBImntRT*, BOMBPortfolio*, my_sortundPortM ap;

Thanks for the reply

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

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

};

std::multimap<B OMBImntRT*, BOMBPortfolio*, UnderlyingsPtrS orter>
undPortMap;
std::multimap<B OMBImntRT*, BOMBPosition*, UnderlyingsPtrS orter>
undPosMap;

std::multimap<B OMBImntRT*, BOMBPortfolio*,
UnderlyingsPtrS orter::iterator undPortIt;
std::multimap<B OMBImntRT*, BOMBPosition*,
UnderlyingsPtrS orter::iterator undPosIt;

I'm getting these compiling errors:

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

BOMBPosition *::_Node *__thiscall std::_Tree<clas s BOMBIm
ntRT *,struct std::pair<class BOMBImntRT * const,class BOMBPosition
*>,struct std::multimap<c lass BOMBImntRT *,class BOMBPosition *,struct
UnderlyingsPtrS orter,class std::allocator< class BOMBPosition *>>::_Kfn,struc t UnderlyingsPtrS orter,class std:

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

BOMBPosition *::_Node *__thiscall std::_Tree<clas s BOMBIm
ntRT *,struct std::pair<class BOMBImntRT * const,class BOMBPosition
*>,struct std::multimap<c lass BOMBImntRT *,class BOMBPosition *,struct
UnderlyingsPtrS orter,class std::allocator< class BOMBPosition *>>::_Kfn,struc t UnderlyingsPtrS orter,class std:

:allocator<clas s BOMBPosition *::_Lbound(clas s BOMBImntRT *const
& ) const'
Error executing cl.exe.- Hide quoted text -

- Show quoted text -
Never mind ... It worked after I add the const
struct UnderlyingsPtrS orter
{
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()(BOMB ImntRT * a, BOMBImntRT * b) {
bool operator()(cons t BOMBImntRT * a, const BOMBImntRT * b) const {
return a->some_value < b->some_value;
}
};
std::multimap<B OMBImntRT*, BOMBPortfolio*, my_sortundPortM ap;
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(BOMBImn tRT* a, BOMBImntRT* b);

or

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

hth

Apr 11 '07 #9
On Apr 11, 6:34 pm, "Marco Wahl" <marco.w...@gma il.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()(BOMB ImntRT * a, BOMBImntRT * b) {
bool operator()(cons t BOMBImntRT * a, const BOMBImntRT * b) const {
return a->some_value < b->some_value;
}
>};
>std::multimap< BOMBImntRT*, BOMBPortfolio*, my_sortundPortM ap;
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(BOMBImn tRT* a, BOMBImntRT* b);

or

struct my_sort
{
void operator(BOMBIm ntRT*, BOMBImntRT*);
void operator()(BOMB ImntRT*, BOMBImntRT*);
>
}
Quick fix, sorry.

Apr 11 '07 #10

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

Similar topics

2
6322
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
3514
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 to class Conception instead? How can I do that? And how should I write to declare an iterator to this vector?
6
9988
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 addresses). Or will it work as I intended? Do you know ways to circumvent the problem? Thanks, Matthias -- Für emails Anweisung in der Adresse...
6
3106
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
3028
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 frequently, I thought it'd be a better idea to manage additional containers which are initialized with pointers to the objects in the primary...
9
1672
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>? To be specific, here is my situation, I have 2 classes A and B. Class A contains a private vector of pointers to B objects and I need to sort this
23
7372
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 in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion....
22
3153
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 header it looks like this (private section) vector<Directory*Directories;
11
656
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 seen this, but a coworker says he is. NB: I can't post sample code that reproduces the issue, nor do I claim any bug in the STL implementation (GCC...
0
7619
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7930
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5514
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
950
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.