multimap and user defined object

P

placid

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
 
K

Kai-Uwe Bux

placid said:
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

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
 

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,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top