STL unordered_set/unordered_map 不能默认hash vector?

news/2024/4/19 17:48:15/

背景

最近在做项目的过程中,遇到使用unordered_set 使用的一个bug,这里简单来记录一下。

例子

#include <iostream>
#include <vector>
#include <set>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <unordered_map>
#include <cmath>
#include <unordered_set>using namespace std;int main(int argc,char* argv[])
{//set<vector<int>> result;//unordered_map<vector<int>,int> result;unordered_set<vector<int>> result;return 0;
}

编译上述代码会产生以下错误。

test.cpp: In function ‘int main(int, char**)’:
test.cpp:18:32: error: use of deleted function ‘std::unordered_set<_Value, _Hash, _Pred, _Alloc>::unordered_set() [with _Value = std::vector<int>; _Hash = std::hash<std::vector<int> >; _Pred = std::equal_to<std::vector<int> >; _Alloc = std::allocator<std::vector<int> >]’18 |     unordered_set<vector<int>> result;|                                ^~~~~~
In file included from /usr/include/c++/11/unordered_set:47,from test.cpp:10:
/usr/include/c++/11/bits/unordered_set.h:135:7: note: ‘std::unordered_set<_Value, _Hash, _Pred, _Alloc>::unordered_set() [with _Value = std::vector<int>; _Hash = std::hash<std::vector<int> >; _Pred = std::equal_to<std::vector<int> >; _Alloc = std::allocator<std::vector<int> >]’ is implicitly deleted because the default definition would be ill-formed:135 |       unordered_set() = default;|       ^~~~~~~~~~~~~
/usr/include/c++/11/bits/unordered_set.h:135:7: error: use of deleted function ‘std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::_Hashtable() [with _Key = std::vector<int>; _Value = std::vector<int>; _Alloc = std::allocator<std::vector<int> >; _ExtractKey = std::__detail::_Identity; _Equal = std::equal_to<std::vector<int> >; _Hash = std::hash<std::vector<int> >; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, true, true>]’
In file included from /usr/include/c++/11/unordered_map:46,from /usr/include/c++/11/functional:61,from /usr/include/c++/11/pstl/glue_algorithm_defs.h:13,from /usr/include/c++/11/algorithm:74,from test.cpp:5:
/usr/include/c++/11/bits/hashtable.h:528:7: note: ‘std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::_Hashtable() [with _Key = std::vector<int>; _Value = std::vector<int>; _Alloc = std::allocator<std::vector<int> >; _ExtractKey = std::__detail::_Identity; _Equal = std::equal_to<std::vector<int> >; _Hash = std::hash<std::vector<int> >; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, true, true>]’ is implicitly deleted because the default definition would be ill-formed:528 |       _Hashtable() = default;|       ^~~~~~~~~~
/usr/include/c++/11/bits/hashtable.h:528:7: error: use of deleted function ‘std::__detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _Traits>::_Hashtable_base() [with _Key = std::vector<int>; _Value = std::vector<int>; _ExtractKey = std::__detail::_Identity; _Equal = std::equal_to<std::vector<int> >; _Hash = std::hash<std::vector<int> >; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; _Traits = std::__detail::_Hashtable_traits<true, true, true>]’
In file included from /usr/include/c++/11/bits/hashtable.h:35,from /usr/include/c++/11/unordered_map:46,from /usr/include/c++/11/functional:61,from /usr/include/c++/11/pstl/glue_algorithm_defs.h:13,from /usr/include/c++/11/algorithm:74,from test.cpp:5:
/usr/include/c++/11/bits/hashtable_policy.h:1604:7: note: ‘std::__detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _Traits>::_Hashtable_base() [with _Key = std::vector<int>; _Value = std::vector<int>; _ExtractKey = std::__detail::_Identity; _Equal = std::equal_to<std::vector<int> >; _Hash = std::hash<std::vector<int> >; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; _Traits = std::__detail::_Hashtable_traits<true, true, true>]’ is implicitly deleted because the default definition would be ill-formed:1604 |       _Hashtable_base() = default;|       ^~~~~~~~~~~~~~~
/usr/include/c++/11/bits/hashtable_policy.h:1604:7: error: use of deleted function ‘std::__detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Hash, _RangeHash, _Unused, __cache_hash_code>::_Hash_code_base() [with _Key = std::vector<int>; _Value = std::vector<int>; _ExtractKey = std::__detail::_Identity; _Hash = std::hash<std::vector<int> >; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; bool __cache_hash_code = true]’
/usr/include/c++/11/bits/hashtable_policy.h: In instantiation of ‘std::__detail::_Hashtable_ebo_helper<_Nm, _Tp, true>::_Hashtable_ebo_helper() [with int _Nm = 1; _Tp = std::hash<std::vector<int> >]’:
/usr/include/c++/11/bits/hashtable_policy.h:1210:7:   required from here
/usr/include/c++/11/bits/hashtable_policy.h:1127:49: error: use of deleted function ‘std::hash<std::vector<int> >::hash()’1127 |       _Hashtable_ebo_helper() noexcept(noexcept(_Tp())) : _Tp() { }|                                                 ^~~~~
In file included from /usr/include/c++/11/string_view:42,from /usr/include/c++/11/bits/basic_string.h:48,from /usr/include/c++/11/string:55,from /usr/include/c++/11/bits/locale_classes.h:40,from /usr/include/c++/11/bits/ios_base.h:41,from /usr/include/c++/11/ios:42,from /usr/include/c++/11/ostream:38,from /usr/include/c++/11/iostream:39,from test.cpp:1:
/usr/include/c++/11/bits/functional_hash.h:102:12: note: ‘std::hash<std::vector<int> >::hash()’ is implicitly deleted because the default definition would be ill-formed:102 |     struct hash : __hash_enum<_Tp>|            ^~~~
/usr/include/c++/11/bits/functional_hash.h:102:12: error: no matching function for call to ‘std::__hash_enum<std::vector<int>, false>::__hash_enum()’
/usr/include/c++/11/bits/functional_hash.h:83:7: note: candidate: ‘std::__hash_enum<_Tp, <anonymous> >::__hash_enum(std::__hash_enum<_Tp, <anonymous> >&&) [with _Tp = std::vector<int>; bool <anonymous> = false]’83 |       __hash_enum(__hash_enum&&);|       ^~~~~~~~~~~
/usr/include/c++/11/bits/functional_hash.h:83:7: note:   candidate expects 1 argument, 0 provided
/usr/include/c++/11/bits/functional_hash.h:102:12: error: ‘std::__hash_enum<_Tp, <anonymous> >::~__hash_enum() [with _Tp = std::vector<int>; bool <anonymous> = false]’ is private within this context102 |     struct hash : __hash_enum<_Tp>|            ^~~~
/usr/include/c++/11/bits/functional_hash.h:84:7: note: declared private here84 |       ~__hash_enum();|       ^
In file included from /usr/include/c++/11/bits/hashtable.h:35,from /usr/include/c++/11/unordered_map:46,from /usr/include/c++/11/functional:61,from /usr/include/c++/11/pstl/glue_algorithm_defs.h:13,from /usr/include/c++/11/algorithm:74,from test.cpp:5:
/usr/include/c++/11/bits/hashtable_policy.h:1210:7: note: ‘std::__detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Hash, _RangeHash, _Unused, __cache_hash_code>::_Hash_code_base() [with _Key = std::vector<int>; _Value = std::vector<int>; _ExtractKey = std::__detail::_Identity; _Hash = std::hash<std::vector<int> >; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; bool __cache_hash_code = true]’ is implicitly deleted because the default definition would be ill-formed:1210 |       _Hash_code_base() = default;|       ^~~~~~~~~~~~~~~
/usr/include/c++/11/bits/hashtable_policy.h:1210:7: error: use of deleted function ‘std::__detail::_Hashtable_ebo_helper<1, std::hash<std::vector<int> >, true>::~_Hashtable_ebo_helper()’
/usr/include/c++/11/bits/hashtable_policy.h:1124:12: note: ‘std::__detail::_Hashtable_ebo_helper<1, std::hash<std::vector<int> >, true>::~_Hashtable_ebo_helper()’ is implicitly deleted because the default definition would be ill-formed:1124 |     struct _Hashtable_ebo_helper<_Nm, _Tp, true>|            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/11/bits/hashtable_policy.h:1124:12: error: use of deleted function ‘std::hash<std::vector<int> >::~hash()’
In file included from /usr/include/c++/11/string_view:42,from /usr/include/c++/11/bits/basic_string.h:48,from /usr/include/c++/11/string:55,from /usr/include/c++/11/bits/locale_classes.h:40,from /usr/include/c++/11/bits/ios_base.h:41,from /usr/include/c++/11/ios:42,from /usr/include/c++/11/ostream:38,from /usr/include/c++/11/iostream:39,from test.cpp:1:
/usr/include/c++/11/bits/functional_hash.h:102:12: note: ‘std::hash<std::vector<int> >::~hash()’ is implicitly deleted because the default definition would be ill-formed:102 |     struct hash : __hash_enum<_Tp>|            ^~~~
/usr/include/c++/11/bits/functional_hash.h:102:12: error: ‘std::__hash_enum<_Tp, <anonymous> >::~__hash_enum() [with _Tp = std::vector<int>; bool <anonymous> = false]’ is private within this context
/usr/include/c++/11/bits/functional_hash.h:84:7: note: declared private here84 |       ~__hash_enum();|       ^
In file included from /usr/include/c++/11/bits/hashtable.h:35,from /usr/include/c++/11/unordered_map:46,from /usr/include/c++/11/functional:61,from /usr/include/c++/11/pstl/glue_algorithm_defs.h:13,from /usr/include/c++/11/algorithm:74,from test.cpp:5:
/usr/include/c++/11/bits/hashtable_policy.h:1604:7: error: use of deleted function ‘std::__detail::_Hash_code_base<std::vector<int>, std::vector<int>, std::__detail::_Identity, std::hash<std::vector<int> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>::~_Hash_code_base()’1604 |       _Hashtable_base() = default;|       ^~~~~~~~~~~~~~~
/usr/include/c++/11/bits/hashtable_policy.h:1188:12: note: ‘std::__detail::_Hash_code_base<std::vector<int>, std::vector<int>, std::__detail::_Identity, std::hash<std::vector<int> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>::~_Hash_code_base()’ is implicitly deleted because the default definition would be ill-formed:1188 |     struct _Hash_code_base|            ^~~~~~~~~~~~~~~
/usr/include/c++/11/bits/hashtable_policy.h:1188:12: error: use of deleted function ‘std::__detail::_Hashtable_ebo_helper<1, std::hash<std::vector<int> >, true>::~_Hashtable_ebo_helper()’
In file included from /usr/include/c++/11/unordered_map:46,from /usr/include/c++/11/functional:61,from /usr/include/c++/11/pstl/glue_algorithm_defs.h:13,from /usr/include/c++/11/algorithm:74,from test.cpp:5:
/usr/include/c++/11/bits/hashtable.h:528:7: error: use of deleted function ‘std::__detail::_Hashtable_base<std::vector<int>, std::vector<int>, std::__detail::_Identity, std::equal_to<std::vector<int> >, std::hash<std::vector<int> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits<true, true, true> >::~_Hashtable_base()’528 |       _Hashtable() = default;|       ^~~~~~~~~~
In file included from /usr/include/c++/11/bits/hashtable.h:35,from /usr/include/c++/11/unordered_map:46,from /usr/include/c++/11/functional:61,from /usr/include/c++/11/pstl/glue_algorithm_defs.h:13,from /usr/include/c++/11/algorithm:74,from test.cpp:5:
/usr/include/c++/11/bits/hashtable_policy.h:1561:12: note: ‘std::__detail::_Hashtable_base<std::vector<int>, std::vector<int>, std::__detail::_Identity, std::equal_to<std::vector<int> >, std::hash<std::vector<int> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits<true, true, true> >::~_Hashtable_base()’ is implicitly deleted because the default definition would be ill-formed:1561 |     struct _Hashtable_base|            ^~~~~~~~~~~~~~~
/usr/include/c++/11/bits/hashtable_policy.h:1561:12: error: use of deleted function ‘std::__detail::_Hash_code_base<std::vector<int>, std::vector<int>, std::__detail::_Identity, std::hash<std::vector<int> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>::~_Hash_code_base()’
In file included from /usr/include/c++/11/unordered_map:46,from /usr/include/c++/11/functional:61,from /usr/include/c++/11/pstl/glue_algorithm_defs.h:13,from /usr/include/c++/11/algorithm:74,from test.cpp:5:
/usr/include/c++/11/bits/hashtable.h:528:7: error: use of deleted function ‘constexpr std::_Enable_default_constructor<false, _Tag>::_Enable_default_constructor() [with _Tag = std::__detail::_Hash_node_base]’528 |       _Hashtable() = default;|       ^~~~~~~~~~
In file included from /usr/include/c++/11/bits/hashtable.h:36,from /usr/include/c++/11/unordered_map:46,from /usr/include/c++/11/functional:61,from /usr/include/c++/11/pstl/glue_algorithm_defs.h:13,from /usr/include/c++/11/algorithm:74,from test.cpp:5:
/usr/include/c++/11/bits/enable_special_members.h:113:15: note: declared here113 |     constexpr _Enable_default_constructor() noexcept = delete;|               ^~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/11/unordered_map:46,from /usr/include/c++/11/functional:61,from /usr/include/c++/11/pstl/glue_algorithm_defs.h:13,from /usr/include/c++/11/algorithm:74,from test.cpp:5:
/usr/include/c++/11/bits/hashtable.h: In instantiation of ‘std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::~_Hashtable() [with _Key = std::vector<int>; _Value = std::vector<int>; _Alloc = std::allocator<std::vector<int> >; _ExtractKey = std::__detail::_Identity; _Equal = std::equal_to<std::vector<int> >; _Hash = std::hash<std::vector<int> >; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, true, true>]’:
/usr/include/c++/11/bits/unordered_set.h:97:11:   required from here
/usr/include/c++/11/bits/hashtable.h:1534:5: error: use of deleted function ‘std::__detail::_Hashtable_base<std::vector<int>, std::vector<int>, std::__detail::_Identity, std::equal_to<std::vector<int> >, std::hash<std::vector<int> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits<true, true, true> >::~_Hashtable_base()’1534 |     }

看这一段报错,是没有进行初始化导致的。

test.cpp: In function ‘int main(int, char**)’:
test.cpp:18:32: error: use of deleted function ‘std::unordered_set<_Value, _Hash, _Pred, _Alloc>::unordered_set() [with _Value = std::vector<int>; _Hash = std::hash<std::vector<int> >; _Pred = std::equal_to<std::vector<int> >; _Alloc = std::allocator<std::vector<int> >]’18 |     unordered_set<vector<int>> result;|                                ^~~~~~
In file included from /usr/include/c++/11/unordered_set:47,from test.cpp:10:
/usr/include/c++/11/bits/unordered_set.h:135:7: note: ‘std::unordered_set<_Value, _Hash, _Pred, _Alloc>::unordered_set() [with _Value = std::vector<int>; _Hash = std::hash<std::vector<int> >; _Pred = std::equal_to<std::vector<int> >; _Alloc = std::allocator<std::vector<int> >]’ is implicitly deleted because the default definition would be ill-formed:135 |       unordered_set() = default;

修改代码进行默认初始化,代码如下所示:

#include <iostream>
#include <vector>
#include <set>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <unordered_map>
#include <cmath>
#include <unordered_set>using namespace std;int main(int argc,char* argv[])
{//set<vector<int>> result;//unordered_map<vector<int>,int> result;unordered_set<vector<int>> result(0);return 0;
}

这个时候,编译器没有红色下划线提示了。但是具体对代码进行编译,还是会报以下错误。

In file included from /usr/include/c++/11/unordered_set:47,from test.cpp:10:
/usr/include/c++/11/bits/unordered_set.h: In constructor ‘std::unordered_set<_Value, _Hash, _Pred, _Alloc>::unordered_set(std::unordered_set<_Value, _Hash, _Pred, _Alloc>::size_type, const hasher&, const key_equal&, const allocator_type&) [with _Value = std::vector<int>; _Hash = std::hash<std::vector<int> >; _Pred = std::equal_to<std::vector<int> >; _Alloc = std::allocator<std::vector<int> >; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::size_type = long unsigned int; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::hasher = std::hash<std::vector<int> >; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::key_equal = std::equal_to<std::vector<int> >; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::allocator_type = std::allocator<std::vector<int> >]’:
/usr/include/c++/11/bits/unordered_set.h:146:42: error: use of deleted function ‘std::hash<std::vector<int> >::hash()’146 |                     const hasher& __hf = hasher(),|                                          ^~~~~~~~
In file included from /usr/include/c++/11/string_view:42,from /usr/include/c++/11/bits/basic_string.h:48,from /usr/include/c++/11/string:55,from /usr/include/c++/11/bits/locale_classes.h:40,from /usr/include/c++/11/bits/ios_base.h:41,from /usr/include/c++/11/ios:42,from /usr/include/c++/11/ostream:38,from /usr/include/c++/11/iostream:39,from test.cpp:1:
/usr/include/c++/11/bits/functional_hash.h:102:12: note: ‘std::hash<std::vector<int> >::hash()’ is implicitly deleted because the default definition would be ill-formed:102 |     struct hash : __hash_enum<_Tp>|            ^~~~
/usr/include/c++/11/bits/functional_hash.h:102:12: error: no matching function for call to ‘std::__hash_enum<std::vector<int>, false>::__hash_enum()’
/usr/include/c++/11/bits/functional_hash.h:83:7: note: candidate: ‘std::__hash_enum<_Tp, <anonymous> >::__hash_enum(std::__hash_enum<_Tp, <anonymous> >&&) [with _Tp = std::vector<int>; bool <anonymous> = false]’83 |       __hash_enum(__hash_enum&&);|       ^~~~~~~~~~~
/usr/include/c++/11/bits/functional_hash.h:83:7: note:   candidate expects 1 argument, 0 provided
/usr/include/c++/11/bits/functional_hash.h:102:12: error: ‘std::__hash_enum<_Tp, <anonymous> >::~__hash_enum() [with _Tp = std::vector<int>; bool <anonymous> = false]’ is private within this context102 |     struct hash : __hash_enum<_Tp>|            ^~~~
/usr/include/c++/11/bits/functional_hash.h:84:7: note: declared private here84 |       ~__hash_enum();|       ^
test.cpp:18:40: note:   when instantiating default argument for call to ‘std::unordered_set<_Value, _Hash, _Pred, _Alloc>::unordered_set(std::unordered_set<_Value, _Hash, _Pred, _Alloc>::size_type, const hasher&, const key_equal&, const allocator_type&) [with _Value = std::vector<int>; _Hash = std::hash<std::vector<int> >; _Pred = std::equal_to<std::vector<int> >; _Alloc = std::allocator<std::vector<int> >; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::size_type = long unsigned int; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::hasher = std::hash<std::vector<int> >; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::key_equal = std::equal_to<std::vector<int> >; std::unordered_set<_Value, _Hash, _Pred, _Alloc>::allocator_type = std::allocator<std::vector<int> >]’18 |     unordered_set<vector<int>> result(0);|                                        ^
In file included from /usr/include/c++/11/unordered_map:46,from /usr/include/c++/11/functional:61,from /usr/include/c++/11/pstl/glue_algorithm_defs.h:13,from /usr/include/c++/11/algorithm:74,from test.cpp:5:
/usr/include/c++/11/bits/hashtable.h: In instantiation of ‘std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::~_Hashtable() [with _Key = std::vector<int>; _Value = std::vector<int>; _Alloc = std::allocator<std::vector<int> >; _ExtractKey = std::__detail::_Identity; _Equal = std::equal_to<std::vector<int> >; _Hash = std::hash<std::vector<int> >; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, true, true>]’:
/usr/include/c++/11/bits/unordered_set.h:97:11:   required from here
/usr/include/c++/11/bits/hashtable.h:1534:5: error: use of deleted function ‘std::__detail::_Hashtable_base<std::vector<int>, std::vector<int>, std::__detail::_Identity, std::equal_to<std::vector<int> >, std::hash<std::vector<int> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits<true, true, true> >::~_Hashtable_base()’1534 |     }|     ^
In file included from /usr/include/c++/11/bits/hashtable.h:35,from /usr/include/c++/11/unordered_map:46,from /usr/include/c++/11/functional:61,from /usr/include/c++/11/pstl/glue_algorithm_defs.h:13,from /usr/include/c++/11/algorithm:74,from test.cpp:5:
/usr/include/c++/11/bits/hashtable_policy.h:1561:12: note: ‘std::__detail::_Hashtable_base<std::vector<int>, std::vector<int>, std::__detail::_Identity, std::equal_to<std::vector<int> >, std::hash<std::vector<int> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits<true, true, true> >::~_Hashtable_base()’ is implicitly deleted because the default definition would be ill-formed:1561 |     struct _Hashtable_base|            ^~~~~~~~~~~~~~~
/usr/include/c++/11/bits/hashtable_policy.h:1561:12: error: use of deleted function ‘std::__detail::_Hash_code_base<std::vector<int>, std::vector<int>, std::__detail::_Identity, std::hash<std::vector<int> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>::~_Hash_code_base()’
/usr/include/c++/11/bits/hashtable_policy.h:1188:12: note: ‘std::__detail::_Hash_code_base<std::vector<int>, std::vector<int>, std::__detail::_Identity, std::hash<std::vector<int> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>::~_Hash_code_base()’ is implicitly deleted because the default definition would be ill-formed:1188 |     struct _Hash_code_base|            ^~~~~~~~~~~~~~~
/usr/include/c++/11/bits/hashtable_policy.h:1188:12: error: use of deleted function ‘std::__detail::_Hashtable_ebo_helper<1, std::hash<std::vector<int> >, true>::~_Hashtable_ebo_helper()’
/usr/include/c++/11/bits/hashtable_policy.h:1124:12: note: ‘std::__detail::_Hashtable_ebo_helper<1, std::hash<std::vector<int> >, true>::~_Hashtable_ebo_helper()’ is implicitly deleted because the default definition would be ill-formed:1124 |     struct _Hashtable_ebo_helper<_Nm, _Tp, true>|            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/11/bits/hashtable_policy.h:1124:12: error: use of deleted function ‘std::hash<std::vector<int> >::~hash()’
In file included from /usr/include/c++/11/string_view:42,from /usr/include/c++/11/bits/basic_string.h:48,from /usr/include/c++/11/string:55,from /usr/include/c++/11/bits/locale_classes.h:40,from /usr/include/c++/11/bits/ios_base.h:41,from /usr/include/c++/11/ios:42,from /usr/include/c++/11/ostream:38,from /usr/include/c++/11/iostream:39,from test.cpp:1:
/usr/include/c++/11/bits/functional_hash.h:102:12: note: ‘std::hash<std::vector<int> >::~hash()’ is implicitly deleted because the default definition would be ill-formed:102 |     struct hash : __hash_enum<_Tp>|            ^~~~
/usr/include/c++/11/bits/functional_hash.h:102:12: error: ‘std::__hash_enum<_Tp, <anonymous> >::~__hash_enum() [with _Tp = std::vector<int>; bool <anonymous> = false]’ is private within this context
/usr/include/c++/11/bits/functional_hash.h:84:7: note: declared private here84 |       ~__hash_enum();|       ^

这个错误是因为 `std::unordered_set` 要求元素类型必须支持哈希,而 `std::vector<int>` 并没有默认的哈希函数。

解决方案一

自行为vector定义hash操作,如下代码:

#include <iostream>
#include <vector>
#include <set>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <unordered_map>
#include <cmath>
#include <unordered_set>using namespace std;int main(int argc,char* argv[])
{//set<vector<int>> result;//unordered_map<vector<int>,int> result;struct VectorHash {size_t operator()(const std::vector<int>& v) const {std::hash<int> hasher;size_t seed = 0;for (int i : v) {seed ^= hasher(i) + 0x9e3779b9 + (seed<<6) + (seed>>2);}return seed;}};unordered_set<vector<int>,VectorHash> result;return 0;
}

解决方案二

使用 `std::set` 代替 `std::unordered_set`,因为 `std::set` 不需要元素类型支持哈希。因为`std::set`底层是通过红黑树来实现的。std::unordered_set`底层是通过哈希表来实现的。代码如下所示:

#include <iostream>
#include <vector>
#include <set>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <unordered_map>
#include <cmath>
#include <unordered_set>using namespace std;int main(int argc,char* argv[])
{//set<vector<int>> result;//unordered_map<vector<int>,int> result;set<vector<int>> result;return 0;
}


http://www.ppmy.cn/news/1365122.html

相关文章

uniapp的扩展组件uni-popup 弹出层自动打开

我的需求是在页面加载完之后自动打开弹窗&#xff0c;自动打开只能写在onReady 或 mounted 生命周期内&#xff0c;这是这个组件的规定&#xff1a; 如果想在页面渲染完毕后就打开 uni-popup &#xff0c;请在 onReady 或 mounted 生命周期内调用&#xff0c;确保组件渲染完毕…

【一】【计算机网络】win基本命令

检查自己的信息 以太网IPv4 地址就是本地计算机。 以太网适配器 VMware Network Adapter VMnet1的IPv4 地址不是本地计算机。局域网内其他的计算机。 C:\Users\205>ipconfigWindows IP 配置以太网适配器 以太网:连接特定的 DNS 后缀 . . . . . . . :本地链接 IPv6 地址. .…

day02_前后端环境搭建(前端工程搭建,登录功能说明,后端项目搭建)

文章目录 1. 软件开发介绍1.1 软件开发流程1.2 角色分工1.3 软件环境1.4 系统的分类 2. 尚品甄选项目介绍2.1 电商基本概念2.1.1 电商简介2.1.2 电商模式B2BB2CB2B2CC2BC2CO2O 2.2 业务功能介绍2.3 系统架构介绍2.4 前后端分离开发 3. 前端工程搭建3.1 Element-Admin简介3.2 El…

目标跟踪之KCF详解

High-Speed Tracking with Kernelized Correlation Filters 使用内核化相关滤波器进行高速跟踪 大多数现代跟踪器的核心组件是判别分类器&#xff0c;其任务是区分目标和周围环境。为了应对自然图像变化&#xff0c;此分类器通常使用平移和缩放的样本补丁进行训练。此类样本集…

Eureka服务搭建

1️⃣搭建服务 引入依赖 <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency>启动类加注解 EnableEurekaServer SpringBootApplication public…

【从零开始学习重要知识点 | 第一篇】快速了解什么是幂等性以及常见解决方案

前言&#xff1a; 当我们在设计和实现分布式系统时&#xff0c;幂等性是一个非常重要的概念。幂等性可以简单地理解为&#xff1a;对于同一操作&#xff0c;不论执行多少次&#xff0c;产生的影响都是相同的。这个概念在分布式系统中非常重要&#xff0c;因为在这种环境下&…

【机器人最短路径规划问题(栅格地图)】基于蚁群算法求解

基于蚁群算法求解机器人最短路径规划问题的仿真结果 仿真结果 收敛曲线变化趋势 蚁群算法求解最优解的机器人运动路径 各代蚂蚁求解机器人最短路径的运动轨迹

利用Spring Boot实现MQTT在物联网中的应用

在物联网&#xff08;IoT&#xff09;领域&#xff0c;消息队列遵循发布/订阅模型的MQTT&#xff08;Message Queuing Telemetry Transport&#xff09;协议变得越来越受欢迎。本文将深入探讨如何在Spring Boot中使用MQTT&#xff0c;并讨论其与其他中间件的集成以及在物联网中…

【vue】vue 是怎么把 template 模版编译成 render 函数的,什么是AST抽象语法树

什么是AST 抽象语法树 是一个对象/或者json是一个数据结构 AST通常是由多个节点组成的树状结构&#xff0c;每个节点代表一个语法单位或表达式。节点之间的关系通过父子关系或兄弟关系来表示程序的结构。在不同的编程语言和工具中&#xff0c;AST可能有不同的表示方式和节点类…

【深度学习笔记】深度卷积神经网络——NiN

网络中的网络&#xff08;NiN&#xff09; LeNet、AlexNet和VGG都有一个共同的设计模式&#xff1a;通过一系列的卷积层与汇聚层来提取空间结构特征&#xff1b;然后通过全连接层对特征的表征进行处理。 AlexNet和VGG对LeNet的改进主要在于如何扩大和加深这两个模块。 或者&am…

FPGA 与 数字电路的关系 - 这篇文章 将 持续 更新 :)

先说几个逻辑&#xff1a;&#xff08;强调一下在这篇文章 输入路数 只有 1个或2个&#xff0c;输出只有1个&#xff0c;N个输入M个输出以后再说&#xff09; 看下面的几个图&#xff1a; 图一&#xff08; 忘了 这是 啥门&#xff0c;不是门吧 &#xff1a;&#xff09;也就…

Swagger接口文档管理工具

Swagger 1、Swagger1.1 swagger介绍1.2 项目集成swagger流程1.3 项目集成swagger 2、knife4j2.1 knife4j介绍2.2 项目集成knife4j 1、Swagger 1.1 swagger介绍 官网&#xff1a;https://swagger.io/ Swagger 是一个规范和完整的Web API框架&#xff0c;用于生成、描述、调用和…

Day03:Web架构OSS存储负载均衡CDN加速反向代理WAF防护

目录 WAF CDN OSS 反向代理 负载均衡 思维导图 章节知识点&#xff1a; 应用架构&#xff1a;Web/APP/云应用/三方服务/负载均衡等 安全产品&#xff1a;CDN/WAF/IDS/IPS/蜜罐/防火墙/杀毒等 渗透命令&#xff1a;文件上传下载/端口服务/Shell反弹等 抓包技术&#xff1a…

迭代器模式(Iterator Pattern)

定义 迭代器模式&#xff08;Iterator Pattern&#xff09;是一种行为型设计模式&#xff0c;它提供了一种方法来顺序访问聚合对象中的各个元素&#xff0c;而不需要暴露该对象的内部表示。迭代器模式使得客户端代码能够独立于聚合对象的具体实现进行遍历操作。 在迭代器模式…

SD-WAN技术:优化国内外服务器访问的关键

在全球化的商业环境中&#xff0c;企业经常需要在国内访问国外的服务器。然而&#xff0c;由于地理位置和网络架构的限制&#xff0c;这种跨国访问往往会遇到速度慢、延迟高等问题。SD-WAN&#xff08;软件定义广域网&#xff09;技术的兴起&#xff0c;为企业提供了一种新的解…

sql 分割字段,并分行

创建测试表格 CREATE TABLE test (id INT PRIMARY KEY, data VARCHAR(100)); INSERT INTO test VALUES (1, A,B,C); INSERT INTO test VALUES (2, D,E,F,G);查询并分割字段 SELECT id, value AS split_data FROM test CROSS APPLY STRING_SPLIT(data, ,) WHERE LEN(value) …

10:00面试,10:05就出来了,问的问题过于变态了。。。

我从一家小公司转投到另一家公司&#xff0c;期待着新的工作环境和机会。然而&#xff0c;新公司的加班文化让我有些始料未及。虽然薪资相对较高&#xff0c;但长时间的工作和缺乏休息使我身心俱疲。 就在我逐渐适应这种高强度的工作节奏时&#xff0c;公司突然宣布了一则令人…

了解 Go 中原子操作的重要性与使用方法

引言 并发是现代软件开发的一个基本方面&#xff0c;而在 Go 中编写并发程序相对来说是一个相对轻松的任务&#xff0c;这要归功于其强大的并发支持。 Go 提供了对原子操作的内置支持&#xff0c;这在同步并发程序中起着至关重要的作用。在本篇博客文章中&#xff0c;我们将探…

最新红盟云卡个人自动发卡开源系统源码优化版

红盟云卡系统是云商学院旗下的一款基于 PHPMySQL 开发的虚拟商品在线售卖平台。它是一款漂亮且功能丰富的发卡网站&#xff0c;可以与社区进行对接。该系统完全开源且无任何加密&#xff0c;可商业使用&#xff0c;并支持个人免签多个接口。 下载地址&#xff1a;优化版.zip

七、ChatGPT为什么会被热炒?

2023年上半年&#xff0c;ChatGPT引起了广泛的热议&#xff0c;对于ChatGPT有多热&#xff0c;不需要我重复了&#xff0c;你可能在网上看到了很多报道&#xff0c;标题如《ChatGPT揭开AI战幔&#xff1a;杀死黄页一样摧毁Google&#xff1f;》和《ChatGPT强势来袭&#xff0c;…