Fun with std::map
-
Hey guys, I'm having some trouble with a std::map object I'm trying to use. Can any STL guys out there see something I'm doing wrong in my code. I've been debugging this for a couple hours now, stepping through STL code (which isn't fun!). All I'm trying to do is associate a structure with a string. So, I insert a value_type with a string and a structure, then I try to look up the exact same string, and I get an invalid iterator. One wierd thing, is that it works perfectly for the first item I insert. Looking at the guts of the tree, I can see that the second object is there, it just checks the wrong side of the tree when it goes to look it up :confused:.
// Some definitions...
typedef basic_string<TCHAR> CTCharString;
struct CTCharStringLess : public binary_function<CTCharString, CTCharString, bool>
{
bool operator()(const CTCharString& x, const CTCharString& y) const
{
return _tcscmp( x.c_str(), y.c_str() );
}
};typedef struct tagFoo
{
long Bar;
}Foo;typedef map<CTCharString,Foo,CTCharStringLess> CStdMapStringToStruct;
// Inside the code
CStdMapStringToStruct MyMap;
Foo stMyStruct;// Insert the first item.
MyMap.insert( CStdMapStringToStruct::value_type( _T("dummy_item", stMyStruct ) );// This line finds the correct item.
CStdMapStringToStruct::iterator oIter = MyMap.find( _T("dummy_item") );// Insert a second item.
MyMap.insert( CStdMapStringToStruct::value_type( _T("dummy_item_2"), stMyStruct ) );// Why doesn't this line find the item????
oIter = MyMap.find( _T("dummy_item_2") );Thanks, Chris Richardson Programmers find all sorts of ingenious ways to screw ourselves over. - Tim Smith
-
Hey guys, I'm having some trouble with a std::map object I'm trying to use. Can any STL guys out there see something I'm doing wrong in my code. I've been debugging this for a couple hours now, stepping through STL code (which isn't fun!). All I'm trying to do is associate a structure with a string. So, I insert a value_type with a string and a structure, then I try to look up the exact same string, and I get an invalid iterator. One wierd thing, is that it works perfectly for the first item I insert. Looking at the guts of the tree, I can see that the second object is there, it just checks the wrong side of the tree when it goes to look it up :confused:.
// Some definitions...
typedef basic_string<TCHAR> CTCharString;
struct CTCharStringLess : public binary_function<CTCharString, CTCharString, bool>
{
bool operator()(const CTCharString& x, const CTCharString& y) const
{
return _tcscmp( x.c_str(), y.c_str() );
}
};typedef struct tagFoo
{
long Bar;
}Foo;typedef map<CTCharString,Foo,CTCharStringLess> CStdMapStringToStruct;
// Inside the code
CStdMapStringToStruct MyMap;
Foo stMyStruct;// Insert the first item.
MyMap.insert( CStdMapStringToStruct::value_type( _T("dummy_item", stMyStruct ) );// This line finds the correct item.
CStdMapStringToStruct::iterator oIter = MyMap.find( _T("dummy_item") );// Insert a second item.
MyMap.insert( CStdMapStringToStruct::value_type( _T("dummy_item_2"), stMyStruct ) );// Why doesn't this line find the item????
oIter = MyMap.find( _T("dummy_item_2") );Thanks, Chris Richardson Programmers find all sorts of ingenious ways to screw ourselves over. - Tim Smith
-
i suspect : return _tcscmp( x.c_str(), y.c_str() ); should be return _tcscmp( x.c_str(), y.c_str() ) < 0;
Wow, do I feel like a dummy. Of course, your suggestion works perfectly. Thanks! Chris Richardson Programmers find all sorts of ingenious ways to screw ourselves over. - Tim Smith