Connecting Tech Pros Worldwide Help | Site Map

multimap and user defined object

placid
Guest
 
Posts: n/a
#1: Oct 4 '05
Hi all,

i was just wondering if i have a class

class A {};

then i want to use a multimap like,

multimap<string,A*> as; //can i have a pointer to a A object ?
A* aa = new A();
as.insert(make_pair("First A",aa)); //this doesn't work !

the question is can i have a multimap pair of string, and a class that
i have made (pointer to an object) and what was the other way of adding
to a multimap something like,

aa["First A"] = aa; //is this right


Thanks in advance

Kai-Uwe Bux
Guest
 
Posts: n/a
#2: Oct 4 '05

re: multimap and user defined object


placid wrote:
[color=blue]
> Hi all,
>
> i was just wondering if i have a class
>
> class A {};
>
> then i want to use a multimap like,
>
> multimap<string,A*> as; //can i have a pointer to a A object ?
> A* aa = new A();
> as.insert(make_pair("First A",aa)); //this doesn't work !
>
> the question is can i have a multimap pair of string, and a class that
> i have made (pointer to an object) and what was the other way of adding
> to a multimap something like,
>
> aa["First A"] = aa; //is this right[/color]

Very likely, your problem does not arise from the A* in the second
coordinate but from the char* in the first. Your map will not use the
strings as keys but the memory locations of their first characters. Also,
it will not lexicographically compare they keys.

Try std::map< std::string, A* > instead. That should work just fine.


Best

Kai-Uwe Bux
Closed Thread