CMap-结构体键值使用

关于CMap的理论知识可以看下MSDN,关于使用,我关注的约束如下:

使用结构体作为index时,

1. Index的无参构造函数需要有

MyIndex(){}

2. 两个模板函数必须重定义,否则会一直报错(报错原因,缺省下面两个模板定义使用有问题)

template<>

AFX_INLINE UINTAFXAPI HashKey(MyIndex key)

template<>

AFX_INLINE BOOLAFXAPI CompareElements(const MyIndex*key1, const MyIndex*key2)

 

下面是一个精简的样例,定义成这样以后,使用CMap map 可以使用OK了.

struct MyIndex
{
	public:
		CString name;
		unsigned int uiIndex;
		MyIndex(){}
};
template<>
AFX_INLINE UINT AFXAPI HashKey(MyIndex key)
{
	return key.uiIndex;
}
template<>
AFX_INLINE BOOL AFXAPI CompareElements(const MyIndex* pElement1, const MyIndex* pElement2)
{
	if (pElement1->name == pElement1->name && pElement1->uiIndex == pElement1->uiIndex)
	{
		return TRUE;
	}
	return FALSE;	
}


 

 

 

你可能感兴趣的:(MFC)