【发布时间】:2019-10-17 07:55:05
【问题描述】:
当我尝试使用std::unordered_set<CComBSTR>(或std::unordered_set<CAdapt<CComBSTR>>)时,出现错误
c:\apps\vs2017pro\vc\tools\msvc\14.16.27023\include\unordered_set(105) : error C2280 : 'std::hash<_Kty>::hash(const std::hash<_Kty> &)' : attempting to reference a deleted function
with
[
_Kty = ATL::CComBSTR
]
但是std::set<CComBSTR>(或std::set<CAdapt<CComBSTR>>)没问题。我正在使用 Visual Studio 2017。
我要怎么做才能仍然达到 O(1) 的搜索时间复杂度?(当然,我们可以使用自定义哈希函数来实现 O(1) 的搜索时间复杂度。)
下面给出了最小的可重现示例。
#include "atlbase.h"
#include <unordered_set>
#include <set>
int main()
{
//std::unordered_set<CComBSTR> s; // compile error
//std::unordered_set<CAdapt<CComBSTR>> s; // compile error
//std::set<CComBSTR> s; // ok
//std::set<CAdapt<CComBSTR>> s; // ok
return 0;
}
编辑(06/02/2019):
我理解CComBSTR 没有哈希函数的错误,我们可以创建一个自定义的。我想问的是,std::set 有哈希函数但没有std::unordered_set 的设计原因是什么?
【问题讨论】:
-
what is the design reason for std::set to have a hash function but not std::unordered_set?其实是反过来的。std_set被实现为平衡树,这意味着它的内容本质上是排序的,但插入和访问时间都是 O(NlogN)。std::unordered_set实现为哈希表,这意味着这两个操作都是 O(1)。您选择使用哪个取决于您的用例。
标签: c++ visual-studio stl com atl