以下是一个简单的示例,演示如何使用 Lookup 方法:
#include <afxtempl.h> // 包含 MFC 模板类头文件
// 假设我们创建了一个 CMap 类型的哈希表
CMap<int, int, CString, CString> myMap;
// 向哈希表中添加一些键-值对
myMap[1] = _T("One");
myMap[2] = _T("Two");
myMap[3] = _T("Three");
// 查找键为 2 的值
CString strValue;
if (myMap.Lookup(2, strValue)) {
TRACE(_T("The value associated with key 2 is: %s\n"), strValue);
} else {
TRACE(_T("Key 2 not found in the map.\n"));
}
// 查找键为 4 的值
if (myMap.Lookup(4, strValue)) {
TRACE(_T("The value associated with key 4 is: %s\n"), strValue);
} else {
TRACE(_T("Key 4 not found in the map.\n"));
}
在这个示例中,我们使用 Lookup 方法查找键为 2 和 4 的值。如果键存在,就将关联的值复制到提供的变量中,并输出相应的消息。如果键不存在,则输出相应的未找到消息。
Lookup 方法是在哈希表中查找特定键的常见方法,可以方便地判断某个键是否存在,并在存在时获取相应的值。
转载请注明出处:http://www.pingtaimeng.com/article/detail/18657/MFC/CMap