gpt4 book ai didi

c++ - 如何比较CMap Key Case Insensitive?

转载 作者:行者123 更新时间:2023-11-28 05:37:22 27 4
gpt4 key购买 nike

在下面的例子中,

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 这样的方法,其中额外的参数用作比较器,由比较器处理。

最佳答案

我认为您可以从 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&>

关于c++ - 如何比较CMap Key Case Insensitive?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37939001/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com