在我的应用程序中,将CMapStringToString的使用迁移到std::map有什么真正的好处吗?
我不明白,我试图更改代码,现在它无法编译:
7>------ Build started: Project: Meeting Schedule Assistant, Configuration: Release x64 ------
7>CalendarSettingsOutlookPage.cpp
7>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\xhash(112,53): error C2064: term does not evaluate to a function taking 1 arguments
7>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\xhash(135): message : see reference to variable template 'const bool _Nothrow_hash<std::hash<ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > >,ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > >' being compiled
7>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\xhash(135): message : while compiling class template member function 'size_t std::_Uhash_compare<_Kty,_Hasher,_Keyeq>::operator ()<_Kty>(const _Keyty &) noexcept(<expr>) const'
7> with
7> [
7> _Kty=CString,
7> _Hasher=std::hash<CString>,
7> _Keyeq=std::equal_to<CString>,
7> _Keyty=ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>
7> ]
7>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\xhash(1109): message : see reference to variable template 'const bool _Nothrow_hash<std::_Umap_traits<ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::_Uhash_compare<ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::hash<ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > >,std::equal_to<ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > >,std::allocator<std::pair<ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > const ,ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > >,0>,ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > >' being compiled
7>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\xhash(1096): message : while compiling class template member function 'void std::_Hash<std::_Umap_traits<_Kty,_Ty,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>>::clear(void) noexcept'
7> with
7> [
7> _Kty=CString,
7> _Ty=CString,
7> _Hasher=std::hash<CString>,
7> _Keyeq=std::equal_to<CString>,
7> _Alloc=std::allocator<std::pair<const CString,CString>>
7> ]
7>D:\My Programs\2022\MeetSchedAssist\Meeting Schedule Assistant\CalendarSettingsOutlookPage.cpp(156): message : see reference to function template instantiation 'void std::_Hash<std::_Umap_traits<_Kty,_Ty,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>>::clear(void) noexcept' being compiled
7> with
7> [
7> _Kty=CString,
7> _Ty=CString,
7> _Hasher=std::hash<CString>,
7> _Keyeq=std::equal_to<CString>,
7> _Alloc=std::allocator<std::pair<const CString,CString>>
7> ]
7>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\unordered_map(67): message : see reference to class template instantiation 'std::_Hash<std::_Umap_traits<_Kty,_Ty,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>>' being compiled
7> with
7> [
7> _Kty=CString,
7> _Ty=CString,
7> _Hasher=std::hash<CString>,
7> _Keyeq=std::equal_to<CString>,
7> _Alloc=std::allocator<std::pair<const CString,CString>>
7> ]
7>D:\My Programs\2022\MeetSchedAssist\Meeting Schedule Assistant\CalendarSettingsOutlookPage.h(59): message : see reference to class template instantiation 'std::unordered_map<CString,CString,std::hash<CString>,std::equal_to<CString>,std::allocator<std::pair<const CString,CString>>>' being compiled
7>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\xhash(111,44): error C2056: illegal expression
7>Done building project "Meeting Schedule Assistant.vcxproj" -- FAILED.如果我使用std::map<CString, CString>,它可以工作,但是std::unordered_map<CString, CString>抛出了上面的错误。我的用法是:
if (m_mapCalendarList.find(strCalendarName) != m_mapCalendarList.end())if (strCalendarId == m_mapCalendarList[strCalendarName])m_mapCalendarList.clear();变量在标头中定义。
发布于 2021-11-12 14:22:07
MFC散列映射有一些缺点,除了不是C++标准和不可移植之外:
对于std::unordered_map,您需要实现哈希函数,并将其作为相应的模板参数传递。或者专门为CString指定标准哈希。但我更喜欢前者。
而且最好也通过比较功能,那就是进行区域独立的比较.由于使用不知道区域设置的散列,所以无论如何都不能有区域设置感知映射,但是CString::operator==是区域识别的。
由于有HashKey可以使用LPCSTR和CMap使用的LPCWSTR,所以实现自己的与STL兼容的散列是非常简单的。
您可以完全避免处理散列,并使用std::unordered_map<std::string, std::string>。但我想,向std::string的过渡会带来更多的变化。
https://stackoverflow.com/questions/69909978
复制相似问题