C++ 如何比较CMap密钥不区分大小写?
在以下情况下C++ 如何比较CMap密钥不区分大小写?,c++,mfc,C++,Mfc,在以下情况下 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. } typedef-CMa
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.
}
typedef-CMap-mapname和id;
MapNameAndId[“虚拟”]=1;
int-nId=0;
if(mapname和id.Lookup(“dummy”,nId))
{
//它应该返回true,nId应该更新为1;关键案例不同。
}
如何做到这一点?我可以在添加到映射和查找时对键执行大写\小写,但需要一种类似std::map的方法,其中额外的参数函数作为比较器,比较器处理它。最简单的方法是将所有内容存储为小写,然后用小写参数搜索我想你可以从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&>
CMap
您可以将密钥存储为大写或小写,并编写自定义代码将输入的搜索字符串转换为密钥的大小写
不幸的是,与支持自定义比较的std::map
不同,CMap
不提供该级别的自定义:
template< class KEY, class ARG_KEY, class VALUE, class ARG_VALUE >
class CMap : public CObject
template
类CMap:公共对象
正如您所看到的,没有定制比较器的选项
一般来说,即使在MFC应用程序中,我也会鼓励您使用STL容器类模板,其设计要比旧的MFC容器好得多。但是在std::map中,我们可以提供一个参数作为比较函数,它是否可用于CMap?