Re: map.find doesn't find



On Mar 13, 10:21 am, "aleko" <aleko.pet...@xxxxxxxxx> wrote:
Hi,

I ran into an interesting problem yesterday with STL's map, and I'm
hoping someone would help me understand what's going on.

Basically the map.find() method fails to find a match for a key that I
know is in the map. I followed the code to the == operator, and was
quite surprised to find this:

bool operator==(const const_iterator& _Right) const
{
...
return (_Ptr == _Right._Ptr); // ?!

}

The method is comparing pointers to determine if the objects are
equal! In my case the map key is of type const wchar_t* so this is
definitely not what I want. Below is some code that creates a map,
adds a key/value pair, and then tries to find it.

struct SCmdInfo;
typedef int (*TCommandProc)( const SCmdInfo& cmd );
typedef std::map<const wchar_t*, TCommandProc> TCmdMap;

TCmdMap cmdMap;
cmdMap[L"dir"] = cmd_dir;
TCmdMap::iterator it = cmdMap.find( L"dir" ); // it == cmdMap.end()

What am I doing wrong?

Thanks,

Aleko

use 'std::string' or even MFC/ATL`s 'CString' instead of 'wchar_t*' or
do this:

struct my_str_cmp{
bool operator () (const wchar_t* const left,const wchar_t* const
right) const
{
retrun wstrcmp(left,right)<0;
};
};

//pass my_str_cmp as the third argument to map template
typedef std::map<const wchar_t*, TCommandProc, my_str_cmp >
TCmdMap;

.



Relevant Pages

  • map.find doesnt find
    ... I ran into an interesting problem yesterday with STL's map, ... Basically the map.findmethod fails to find a match for a key that I ... In my case the map key is of type const wchar_t* so this is ... struct SCmdInfo; ...
    (microsoft.public.vc.stl)
  • Re: map.find doesnt find
    ... I ran into an interesting problem yesterday with STL's map, ... Basically the map.findmethod fails to find a match for a key that I ... In order to compare strings, ...
    (microsoft.public.vc.stl)
  • Re: using map, list etc. in const methods
    ... >>Is there a proper way to leave the function const? ... return a const version of the map, so that the caller may not change the ... but failed because of a lot of compiler ...
    (comp.lang.cpp)
  • using map, list etc. in const methods
    ... through a "map" of CNode-pointers. ... CBoard::countPiecesconst ... At the moment I'm just using lists, ... maps in lack of a good book or online tutorial discussing the STL in detail. ...
    (comp.lang.cpp)
  • Re: Maps
    ... There are three const there. ... The first, just makes the map a constant, i.e. ... at least the SGI docs don't mention any restrictions like the ... You can only populate this via ...
    (microsoft.public.vc.stl)

Loading