STL unordered_set/unordered_map 不能默认hash vector?

news/2024/12/13 17:49:56/

背景

最近在做项目的过程中,遇到使用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;并讨论其与其他中间件的集成以及在物联网中…