2016-06-21 43 views
0

在以下情况下,如何比较CMap密钥大小写不敏感?

typedef CMap<CString, CString, int, int> MapNameAndId; 

MapNameAndId["Dummy"] = 1; 

int nId = 0; 
if(MapNameAndId.Lookup("dummy", nId)) 
{ 
    // It should return true and nId should get updated to 1; Key Cases are different. 
} 

如何实现这一目标?我可以对大写字母和小写字母进行加密,同时将其添加到地图中,同时进行查找,但需要像std :: map这样的方式,其中额外的参数用作Comparator在Comparator中处理它的功能。

回答

0

最简单的方式将存储一切小写,然后用小写的说法搜索

+0

但在std :: map中,我们可以提供一个参数作为比较函数,它是否可用于CMap? –

1

我想你可以从CString的派生类,并在它,你可以重新定义==操作符,或UINT()运营商,运营商正在计算散列。

class MyString 
{ 
    operator UINT() 
    { 
     return HashKey(CString(*this).MakeUpper().operator LPCWSTR()); 
    } 


    bool operator==(const MyString& otherMyString) const 
    { 
     return (CompareNoCase(*this, otherMyString) == 0); 
    } 
} 

然后将列表声明应该是

CMap<CMyString, CMyString&, int, int&> 
0

可以存储在大写或小写的关键,并编写自定义代码输入的搜索字符串转换成键的情况。

不幸的是,不像std::map支持自定义的比较,CMap不提供定制的该级别:

template< class KEY, class ARG_KEY, class VALUE, class ARG_VALUE > 
class CMap : public CObject 

正如你可以看到,有一个自定义比较没办法。

通常,即使在MFC应用程序中,我也鼓励您使用容器类模板,它比旧的MFC容器设计得更好。