map可以用find根据key来找map,现实现一个简单在MFC中value为字符串的简单map查找。
short CLBMViewerView::findMAPKeyByValue(typedef CMap<unsigned short,unsigned short,CString,CString>& map,char* c)
{
unsigned short key;
CString str;
POSITION pos = map.GetStartPosition();
while(pos!=NULL){
map.GetNextAssoc(pos,key,str);
if(strcmp(str,CString(c))==0)
return key;
}
return -1; //means no match key with value
}
其中POSITION是MFC模板类库中的一个数据类型,这里的作用类似于STL中的Iterator.
GetNextAssoc()的作用是取出当前迭代器位置的key和value,然后向后移动迭代器。
这里通过CString(c)直接将char *转为string,用strcmp比较取出的字符串与待寻找的字符串是否相同。
本文介绍了一种在MFC中使用CMap类查找特定值对应键的方法。通过实现findMAPKeyByValue函数,可以有效地从value为字符串的map中找到对应的key。此方法对于处理MFC应用程序中的数据结构非常有用。
1076

被折叠的 条评论
为什么被折叠?



