From what I have deduced, the std::map::find() method searches the map by comparising pointer address instead of values. Example:
std::string aa = "asd";
const char* a = aa.c_str();
const char* b = "asd";
// m_options is a std::map<const char*, int )
m_options.insert( std::make_pair( a, 0 ) );
if( m_options.find( b ) != m_options.end() ) {
// won't reach this place
}
I am kinda surprised (because I am using primitive types instead of some class) and I think that I have done something wrong, if not then how to force it to use value instead of address?
nullptr :
You are using char * as a key type for the map. For the pointer types, comparison is performed by their address (as the map cannot know that these pointers are NULL-terminated 8-bit strings).\n\nTo achieve your goal, you could create the map with custom compare function, e.g.:\n\nbool MyStringCompare(const char *s1, const char *s2) { \n return strcmp(s1, s2) < 0; \n}\n...\nstd::map<const char*, int, MyStringCompare> m_options;\n\n\nOr consider using std::string as the key type.",
2013-04-14T20:34:52