C++ how to insert array into hash set?

匿名 (未验证) 提交于 2019-12-03 01:39:01

问题:

I need to insert a 1D array into the hashset.

But I got error while compiling.

#include  #include  #include  using namespace std; int hash_comp(const int* state1,const int* state2) {     int result = 0;      for (i = 0; i ,eqArray> closelist; int main(int argc, char** argv) {    const int sn[16] = {1,2,3,4,5,6,0,8,9,10,11,12,13,14,7,15};    closelist.insert(sn);    return 0; } 
/usr/include/c++/4.2.1/ext/hashtable.h: In member function 'size_t __gnu_cxx::hashtable<_val _key="" _hashfcn="" _extractkey="" _equalkey="" _alloc="">::_M_bkt_num_key(const _Key&, size_t) const [with _Val = int*, _Key = int*, _HashFcn = __gnu_cxx::hash, _ExtractKey = std::_Identity, _EqualKey = std::equal_to, _Alloc = std::allocator]': /usr/include/c++/4.2.1/ext/hashtable.h:599:   instantiated from 'size_t __gnu_cxx::hashtable<_val _key="" _hashfcn="" _extractkey="" _equalkey="" _alloc="">::_M_bkt_num(const _Val&, size_t) const [with _Val = int*, _Key = int*, _HashFcn = __gnu_cxx::hash, _ExtractKey = std::_Identity, _EqualKey = std::equal_to, _Alloc = std::allocator]' /usr/include/c++/4.2.1/ext/hashtable.h:1006:   instantiated from 'void __gnu_cxx::hashtable<_val _key="" _hashfcn="" _extractkey="" _equalkey="" _alloc="">::resize(size_t) [with _Val = int*, _Key = int*, _HashFcn = __gnu_cxx::hash, _ExtractKey = std::_Identity, _EqualKey = std::equal_to, _Alloc = std::allocator]' /usr/include/c++/4.2.1/ext/hashtable.h:437:   instantiated from 'std::pair<:_hashtable_iterator _key="" _hashfcn="" _extractkey="" _equalkey="" _alloc="">, bool> __gnu_cxx::hashtable<_val _key="" _hashfcn="" _extractkey="" _equalkey="" _alloc="">::insert_unique(const _Val&) [with _Val = int*, _Key = int*, _HashFcn = __gnu_cxx::hash, _ExtractKey = std::_Identity, _EqualKey = std::equal_to, _Alloc = std::allocator]' /usr/include/c++/4.2.1/ext/hash_set:197:   instantiated from 'std::pair, _EqualKey, _Alloc>::const_iterator, bool> __gnu_cxx::hash_set<_value _hashfcn="" _equalkey="" _alloc="">::insert(const typename __gnu_cxx::hashtable<_value _value="" _hashfcn="" std::_identity="">, _EqualKey, _Alloc>::value_type&) [with _Value = int*, _HashFcn = __gnu_cxx::hash, _EqualKey = std::equal_to, _Alloc = std::allocator]' src/ods2.cpp:677:   instantiated from here 

回答1:

If you use a std::array instead of int*, all your problems will go away. If you have no C++11 compiler, you can use boost::array instead. Also, replace hash_set with unordered_set.

It appears there is no specialization of std::hash for std::arrays, so I wrote my own:

#include  #include   namespace std {     template     struct hash >     {         typedef array argument_type;         typedef size_t result_type;          result_type operator()(const argument_type& a) const         {             hash hasher;             result_type h = 0;             for (result_type i = 0; i  > closelist;  int main() {     std::array sn = {1,2,3,4,5,6,0,8,9,10,11,12,13,14,7,15};     closelist.insert(sn); } 


回答2:

You didn’t post the actual error message, only the trace. That said, it’s probably because your set uses non-const int* while your data is const.

That said, I’d suggest using unordered_set instead of hash_set(either from std::tr1 if your compiler supports that, or from std if your compiler supports C++11 or from Boost), and heed Fred’s advice of using an {std,boost}::array instead of a raw pointer.



回答3:

I don't think that there exists a specialized hash. I added this:

namespace __gnu_cxx{ //I'm not sure what compiler version you used,                      //mine wanted this     template     struct hash{         size_t operator()(const int*a) const{             size_t r = 0;             for (int i=0;i

I also put in some consts and it compiles.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!