首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CMapStringToString对std::map<CString,CString>

CMapStringToString对std::map<CString,CString>
EN

Stack Overflow用户
提问于 2021-11-10 08:32:10
回答 1查看 285关注 0票数 0

在我的应用程序中,将CMapStringToString的使用迁移到std::map有什么真正的好处吗?

我不明白,我试图更改代码,现在它无法编译:

代码语言:javascript
复制
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();

变量在标头中定义。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-12 14:22:07

MFC散列映射有一些缺点,除了不是C++标准和不可移植之外:

  • 不会自动重新计算哈希表。甚至无法在填充时调整哈希表的大小。哈希表的默认大小对您来说可能不是最优的。正如注释中所指出的,
  • 不支持基于范围的
  • 算法,也不支持它们本身不可复制和不可移动的范围算法。移动容器可能是有用的
  • 不支持元素插入的移动语义,这涉及到一些开销

对于std::unordered_map,您需要实现哈希函数,并将其作为相应的模板参数传递。或者专门为CString指定标准哈希。但我更喜欢前者。

而且最好也通过比较功能,那就是进行区域独立的比较.由于使用不知道区域设置的散列,所以无论如何都不能有区域设置感知映射,但是CString::operator==是区域识别的。

由于有HashKey可以使用LPCSTRCMap使用的LPCWSTR,所以实现自己的与STL兼容的散列是非常简单的。

您可以完全避免处理散列,并使用std::unordered_map<std::string, std::string>。但我想,向std::string的过渡会带来更多的变化。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69909978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档