如何实现CString散列函数与std :: unordered_map一起使用? [英] How do I implement a CString hash function for use with std::unordered_map?
本文介绍了如何实现CString散列函数与std :: unordered_map一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要声明:
std :: unordered_map< CString,CString> m_mapMyMap;
但是当我构建我有一个错误告诉我标准C ++不提供哈希函数对于CString,而CString有(LPCSTR)运算符。
如何正确实现一个散列函数CString?
std :: unordered_map使用std :: hash<>不使用(LPCSTR)
运算符。
您需要重新定义散列函数:
template< class T> ; class MyHash;
模板<>
class MyHash< CString> {
public:
size_t operator()(const CString& s)const
{
return std :: hash< std :: string> );
}
};
std :: unordered_map< CString,CString,MyHash> m_mapMyMap;
但是为了更好的性能,使用std :: string代替CString的键。
I want to declare :
std::unordered_map<CString, CString> m_mapMyMap;
But when I build I got an error telling me that the standard C++ doesn't provide a hash function for CString, while CString have the (LPCSTR) operator.
How do I properly implement a hash function for CString?
解决方案
std::unordered_map use std::hash<> that does not use (LPCSTR)
operator.
You need to redefine hash function:
template<class T> class MyHash;
template<>
class MyHash<CString> {
public:
size_t operator()(const CString &s) const
{
return std::hash<std::string>()( (LPCSTR)s );
}
};
std::unordered_map<CString,CString,MyHash> m_mapMyMap;
But for better performance use std::string instead CString for key.
这篇关于如何实现CString散列函数与std :: unordered_map一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文