【发布时间】:2016-07-18 05:11:19
【问题描述】:
有人可以告诉我如何根据特定键获取特定元素。说如果我有一个 CMap ButtonProp; 现在我想访问提供的任何 int 值的属性,该怎么做? 附言第一次使用 CMap。 “ButtonProp.Lookup(int)”够用吗?
【问题讨论】:
标签: c++ visual-studio-2015 mfc
有人可以告诉我如何根据特定键获取特定元素。说如果我有一个 CMap ButtonProp; 现在我想访问提供的任何 int 值的属性,该怎么做? 附言第一次使用 CMap。 “ButtonProp.Lookup(int)”够用吗?
【问题讨论】:
标签: c++ visual-studio-2015 mfc
你基本上有两种选择:
如果您知道该元素存在,则可以使用CMap::operator[]:
否则,您应该使用CMap::Lookup,但用法与您的问题不同。
假设你从
CMap<int,int,CPoint,CPoint> myMap;
myMap.InitHashTable(257);
要找到对应于 3 的元素,您可以使用
myMap[3]
或
CPoint ret;
bool found = myMap.Lookup(3, ret);
如果found 是true,那么ret 就是您的答案。
【讨论】:
下面是简单的代码sn-p,演示CMap容器的用法:
CMap<CString, LPCTSTR, int, int> NameToValueMap;
CString sKey(_T("Test"));
int nValue = 10;
NameToValueMap[sKey] = nValue;
if (NameToValueMap.IsEmpty())
return;
int nVal;
if (NameToValueMap.Lookup(sKey, nVal))
{
// TO DO: do stuff with value here
}
【讨论】: