Re: map.find doesn't find
- From: "freak" <farid.mehrabi@xxxxxxxxx>
- Date: 19 Mar 2007 03:04:01 -0700
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;
.
- References:
- map.find doesn't find
- From: aleko
- map.find doesn't find
- Prev by Date: Re: Erase in a map
- Next by Date: Re: Erase in a map
- Previous by thread: Re: map.find doesn't find
- Next by thread: Strange ostringstream behaviour in Visual C++ 2005
- Index(es):
Relevant Pages
|
Loading