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. map.find() problems

map.find() problems

Scheduled Pinned Locked Moved C / C++ / MFC
debuggingc++graphicshelpquestion
8 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.
  • M Offline
    M Offline
    moobob
    wrote on last edited by
    #1

    I'm having a strange problem with the STL map.find(). I have a function which I search for an item's existance with a find() before inserting. If I don't find it, I insert it. Trouble is, when an item already exists in the map, both the find() and count() never report it. Also, the insert() lets me add a duplicate key. Below find declarations, code, and debug output. // declarations typedef struct ImportDataType { CString Item; CString Value; char Type; long ReplicationID; } ImportDataType; typedef vector dataVector; typedef map importData; map::const_iterator mapIter; int i = dataMap.count(m_currentEID); int j = dataMap.size(); TRACE ("\nIn AddDataItem, b4 map.insert(), size of Map = %d\n",j); TRACE ("Number of items in the Map with key of %s = %d\n",m_currentEID,i); mapIter = dataMap.find(m_currentEID); if (mapIter != dataMap.end()) TRACE ("B4 insert, key found in map!\n"); else TRACE ("B4 insert, key not found in map!\n"); for (mapIter = dataMap.begin(); mapIter != dataMap.end(); mapIter++) { TRACE ("Key from Map = %s\n", (*mapIter).first); } dataMap.insert(make_pair(m_currentEID, dataVector)); mapIter = dataMap.find(m_currentEID); if (mapIter != dataMap.end()) TRACE ("After insert, found key in map!\n"); else TRACE ("After insert, key not found in map!\n"); i = dataMap.count(m_currentEID); j = dataMap.size(); TRACE ("\nIn AddDataItem, after map.insert(), size of Map = %d\n",j); TRACE ("Number of items in the Map with key of %s = %d\n",m_currentEID,i); for (mapIter = dataMap.begin(); mapIter != dataMap.end(); mapIter++) { TRACE ("Key from Map = %s\n", (*mapIter).first); } The partial output from running this shows the problem. Note the key values are being printed before the insert is done. The key 6748 does exist, but the map.count() and map.find() are not reporting it. Note in the last dump of the map keys that 6748 is there twice! What am I doing wrong? In AddDataItem, b4 map.insert(), size of Map = 3 Number of items in the Map with key of 982000006006748 = 0 B4 insert, key not found in map! Key from Map = 982000006003798 Key from Map = 982000007172537 Key from Map = 982000006006748 After insert, found key in map! In AddDataItem, after map.insert(), size of Map = 4 Number of items in the Map with key of 982000006006748 = 1 Key from Map = 982000006003798 Key from Map = 982000007172537 Key fr

    A 1 Reply Last reply
    0
    • M moobob

      I'm having a strange problem with the STL map.find(). I have a function which I search for an item's existance with a find() before inserting. If I don't find it, I insert it. Trouble is, when an item already exists in the map, both the find() and count() never report it. Also, the insert() lets me add a duplicate key. Below find declarations, code, and debug output. // declarations typedef struct ImportDataType { CString Item; CString Value; char Type; long ReplicationID; } ImportDataType; typedef vector dataVector; typedef map importData; map::const_iterator mapIter; int i = dataMap.count(m_currentEID); int j = dataMap.size(); TRACE ("\nIn AddDataItem, b4 map.insert(), size of Map = %d\n",j); TRACE ("Number of items in the Map with key of %s = %d\n",m_currentEID,i); mapIter = dataMap.find(m_currentEID); if (mapIter != dataMap.end()) TRACE ("B4 insert, key found in map!\n"); else TRACE ("B4 insert, key not found in map!\n"); for (mapIter = dataMap.begin(); mapIter != dataMap.end(); mapIter++) { TRACE ("Key from Map = %s\n", (*mapIter).first); } dataMap.insert(make_pair(m_currentEID, dataVector)); mapIter = dataMap.find(m_currentEID); if (mapIter != dataMap.end()) TRACE ("After insert, found key in map!\n"); else TRACE ("After insert, key not found in map!\n"); i = dataMap.count(m_currentEID); j = dataMap.size(); TRACE ("\nIn AddDataItem, after map.insert(), size of Map = %d\n",j); TRACE ("Number of items in the Map with key of %s = %d\n",m_currentEID,i); for (mapIter = dataMap.begin(); mapIter != dataMap.end(); mapIter++) { TRACE ("Key from Map = %s\n", (*mapIter).first); } The partial output from running this shows the problem. Note the key values are being printed before the insert is done. The key 6748 does exist, but the map.count() and map.find() are not reporting it. Note in the last dump of the map keys that 6748 is there twice! What am I doing wrong? In AddDataItem, b4 map.insert(), size of Map = 3 Number of items in the Map with key of 982000006006748 = 0 B4 insert, key not found in map! Key from Map = 982000006003798 Key from Map = 982000007172537 Key from Map = 982000006006748 After insert, found key in map! In AddDataItem, after map.insert(), size of Map = 4 Number of items in the Map with key of 982000006006748 = 1 Key from Map = 982000006003798 Key from Map = 982000007172537 Key fr

      A Offline
      A Offline
      Alexandru Savescu
      wrote on last edited by
      #2

      There is something wrong with your code. How do you sort your items? You have to provide a < operator so the map can insert and lookup your items. And also, please format your code and change < and > with &lt and > so we can see your code better. Best regards, Alexandru Savescu

      M 1 Reply Last reply
      0
      • A Alexandru Savescu

        There is something wrong with your code. How do you sort your items? You have to provide a < operator so the map can insert and lookup your items. And also, please format your code and change < and > with &lt and > so we can see your code better. Best regards, Alexandru Savescu

        M Offline
        M Offline
        moobob
        wrote on last edited by
        #3

        Alex, Thanks for your response, sorry, I don't know how to format my code for the post, can you please tell me how? Since I don't provide a sorting criterion when I define my map, it should use the default less <>? I don't really understand the sorting criterion very well. I can see my items don't appear to be sorted. All I want to do is insert items and find items. How should I specify it? thanks, Bob

        A 2 Replies Last reply
        0
        • M moobob

          Alex, Thanks for your response, sorry, I don't know how to format my code for the post, can you please tell me how? Since I don't provide a sorting criterion when I define my map, it should use the default less <>? I don't really understand the sorting criterion very well. I can see my items don't appear to be sorted. All I want to do is insert items and find items. How should I specify it? thanks, Bob

          A Offline
          A Offline
          Alexandru Savescu
          wrote on last edited by
          #4

          Use the <pre> tags. You can use them on the formatting bar just above the smileys. Also you can preview the messge before it gets posted. In a map items will be sorted by key. If you use int as keys then you need not provide a < operator. But if your are using some structs then you definitely must give a comparison method. Best regards, Alexandru Savescu

          M 1 Reply Last reply
          0
          • A Alexandru Savescu

            Use the <pre> tags. You can use them on the formatting bar just above the smileys. Also you can preview the messge before it gets posted. In a map items will be sorted by key. If you use int as keys then you need not provide a < operator. But if your are using some structs then you definitely must give a comparison method. Best regards, Alexandru Savescu

            M Offline
            M Offline
            moobob
            wrote on last edited by
            #5

            I am using a key of const char *, the data is struct. How would I specify a comparison method for char *? thanks, Bob

            1 Reply Last reply
            0
            • M moobob

              Alex, Thanks for your response, sorry, I don't know how to format my code for the post, can you please tell me how? Since I don't provide a sorting criterion when I define my map, it should use the default less <>? I don't really understand the sorting criterion very well. I can see my items don't appear to be sorted. All I want to do is insert items and find items. How should I specify it? thanks, Bob

              A Offline
              A Offline
              Alexandru Savescu
              wrote on last edited by
              #6

              Taking a closer look the HTML source page I found this

              typedef map<const char *, dataVector> importData;

              So you are using some const char* as string. Using less on them will compare pointers and of course that your map will not look as you expect. You must compare them with strcmp. Or, easier use CString as key. It has a < operator defined that does good job. Best regards, Alexandru Savescu

              M 1 Reply Last reply
              0
              • A Alexandru Savescu

                Taking a closer look the HTML source page I found this

                typedef map<const char *, dataVector> importData;

                So you are using some const char* as string. Using less on them will compare pointers and of course that your map will not look as you expect. You must compare them with strcmp. Or, easier use CString as key. It has a < operator defined that does good job. Best regards, Alexandru Savescu

                M Offline
                M Offline
                moobob
                wrote on last edited by
                #7

                Alex, Making the key a CString worked!!! Thank you very much for your help! Bob

                A 1 Reply Last reply
                0
                • M moobob

                  Alex, Making the key a CString worked!!! Thank you very much for your help! Bob

                  A Offline
                  A Offline
                  Alexandru Savescu
                  wrote on last edited by
                  #8

                  Of course it did, I just explained the reason. If you want to use const char* (maybe for portability) then here is an exameple from the SGI documentation

                  struct ltstr
                  {
                  bool operator()(const char* s1, const char* s2) const
                  {
                  return strcmp(s1, s2) < 0;
                  }
                  };

                  int main()
                  {
                  map <const char*, int, ltstr> mymap;

                  }

                  Best regards, Alexandru Savescu

                  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