map.find() problems
-
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
-
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
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 < and > so we can see your code better. Best regards, 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 < and > so we can see your code better. Best regards, Alexandru Savescu
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
-
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
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 -
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 -
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
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. Usingless
on them will compare pointers and of course that your map will not look as you expect. You must compare them withstrcmp
. Or, easier use CString as key. It has a < operator defined that does good job. Best regards, 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. Usingless
on them will compare pointers and of course that your map will not look as you expect. You must compare them withstrcmp
. Or, easier use CString as key. It has a < operator defined that does good job. Best regards, Alexandru Savescu -
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 documentationstruct 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