map & find()

V

vertigo

Hello
i created map<myclass1*,myclass2*> object.
I have problem with find function. It only works when
myclass1* is string or int (it does not work for char* and myclass1*).
Is it normal or i have made mistake somewhere ?
How does find() compares objects ? Maybe i should have written some
compare() function ?

Thanx
Michal
 
J

John Harrison

vertigo said:
Hello
i created map<myclass1*,myclass2*> object.
I have problem with find function. It only works when
myclass1* is string or int (it does not work for char* and myclass1*).
Is it normal or i have made mistake somewhere ?
How does find() compares objects ?

If the objects are pointers then it compares pointers, which is almost never
what you want to do.
Maybe i should have written some
compare() function ?

Yes or a comparison functor, either can work.

struct Comp
{
bool operator()(const myclass1* x, myclass1* y) const
{
// return true if x is less than y
}
};

map<myclass1*,myclass2*,Comp> my_map;

john
 

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

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top