CMap-结构体键值使用

来源:互联网 发布:淘宝助理选择打印机 编辑:程序博客网 时间:2024/06/05 01:01

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

使用结构体作为index时,

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

MyIndex(){}

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

template<>

AFX_INLINE UINTAFXAPIHashKey(MyIndex key)

template<>

AFX_INLINE BOOLAFXAPICompareElements(const MyIndex*key1,const MyIndex*key2)

 

下面是一个精简的样例,定义成这样以后,使用CMap<MyIndex,MyIndex,MyInfo,Myinfo> 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;}