來自:https://blog.csdn.net/codingEMIPark/article/details/81566988?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
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比較取出的字符串與待尋找的字符串是否相同。
