Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Fun with std::map

Fun with std::map

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structuresquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chris Richardson
    wrote on last edited by
    #1

    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

    A 1 Reply Last reply
    0
    • C Chris Richardson

      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

      A Offline
      A Offline
      AlexO
      wrote on last edited by
      #2

      i suspect : return _tcscmp( x.c_str(), y.c_str() ); should be return _tcscmp( x.c_str(), y.c_str() ) < 0;

      C 1 Reply Last reply
      0
      • A AlexO

        i suspect : return _tcscmp( x.c_str(), y.c_str() ); should be return _tcscmp( x.c_str(), y.c_str() ) < 0;

        C Offline
        C Offline
        Chris Richardson
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups