problem with std::map find()

  • Thread starter Manuel Maria Diaz Gomez
  • Start date
M

Manuel Maria Diaz Gomez

Hi everybody,

This should be trivial, but I just can't see it.
The following statement in a method doesn't work for me:
(there's only one entry in myMap)

map<const char*, int>::const_iterator it = myMap.find(key);

where key is a const char* passed by the class method.

However, if I do:

map<const char*, int>::const_iterator it = registry.begin();

Then the iterator points to the right value, and (*it).first displays the
right key.
I have checked the key value being passed to find() and is correct, so that
shouldn't be a problem...


Any hints?

Cheers



--
========================================================================
Manuel Diaz-Gomez | ATLAS Bldg. 32/SB-008 tel. +41 22 76 76304
CERN EP Division
CH-1211 Geneva 23
SWITZERLAND
========================================================================
 
W

Walter Tross

map<const char*, int>::const_iterator it = myMap.find(key);

Your map compares C-string addresses, not C-string contents
 
J

Jeff Schwab

Manuel said:
Hi everybody,

This should be trivial, but I just can't see it.
The following statement in a method doesn't work for me:
(there's only one entry in myMap)

map<const char*, int>::const_iterator it = myMap.find(key);

where key is a const char* passed by the class method.

However, if I do:

map<const char*, int>::const_iterator it = registry.begin();

Then the iterator points to the right value, and (*it).first displays the
right key.
I have checked the key value being passed to find() and is correct, so that
shouldn't be a problem...


Any hints?

What Walter said.

Use std::string instead of const char* as the key type.
 

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,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top