C ++如何插入数组哈希集合? [英] C++ how to insert array into hash set?
本文介绍了C ++如何插入数组哈希集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要插入一维数组到HashSet的。
不过,在编译时我得到了错误。
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&hash_set.h GT;
使用命名空间std;
INT hash_comp(const int的* STATE1,const int的* STATE2){
INT结果为0; 对于(i = 0; I< 16;我++)
{
如果(STATE1 [I]!= STATE2 [I]){
结果= -1;
} }
返回结果;
}
结构eqArray
{
布尔运算符()(const int的* A1,const int的* A2)常量
{
返回hash_comp(A1,A2)== 0;
}
};
的hash_set< INT *,散列< INT *>,&eqArray GT; closelist;
INT主(INT ARGC,字符** argv的)
{
const int的SN [16] = {1,2,3,4,5,6,0,8,9,10,11,12,13,14,7,15};
closelist.insert(SN);
返回0;
}
/ usr / include目录/ C ++ / 4.2.1 /转/ hashtable.h:在成员函数'为size_t __gnu_cxx ::哈希表< _Val,_key,_HashFcn,_ExtractKey,_EqualKey,_Alloc&GT ; :: _ M_bkt_num_key(常量_key&安培;,为size_t)常量[与_Val = INT *,_key = INT *,_HashFcn = __gnu_cxx ::散列< INT *>中_ExtractKey =的std :: _身份< INT *>中_EqualKey =的std :: equal_to< INT *>中_Alloc =的std ::分配器< INT *>]':
/usr/include/c++/4.2.1/ext/hashtable.h:599:从'为size_t __gnu_cxx ::哈希表&LT实例; _Val,_key,_HashFcn,_ExtractKey,_EqualKey,_Alloc> :: _ M_bkt_num(常量_Val&安培;,为size_t)常量[与_Val = INT *,_key = INT *,_HashFcn = __gnu_cxx ::散列< INT *>中_ExtractKey =的std :: _身份< INT *>中_EqualKey =的std :: equal_to< INT *>中_Alloc =的std ::分配器< INT *>]
/usr/include/c++/4.2.1/ext/hashtable.h:1006:从'无效__gnu_cxx ::哈希表&LT实例; _Val,_key,_HashFcn,_ExtractKey,_EqualKey,_Alloc> ::调整(为size_t)与_Val =为int *,_key = INT *,_HashFcn = __gnu_cxx ::散列< INT *>中_ExtractKey =的std :: _身份< INT *>中_EqualKey =的std :: equal_to< INT *>中_Alloc =的std ::分配器&LT ;为int *>]
/usr/include/c++/4.2.1/ext/hashtable.h:437:从'的std ::对实例化< __ gnu_cxx :: _ Hashtable_iterator< _Val,_key,_HashFcn,_ExtractKey,_EqualKey,_Alloc>中布尔> __gnu_cxx ::哈希表< _Val,_key,_HashFcn,_ExtractKey,_EqualKey,_Alloc> :: insert_unique(常量_Val&安培;)与_Val = INT *,_key = INT *,_HashFcn = __gnu_cxx ::散列< INT *>中_ExtractKey =的std :: _身份< INT *>中_EqualKey =的std :: equal_to< INT *>中_Alloc =的std ::分配器< INT *>]
/usr/include/c++/4.2.1/ext/hash_set:197:从'的std ::对&LT实例化,类型名__gnu_cxx ::哈希表< _Value,_Value,_HashFcn,性病:: _身份< _TP>中_EqualKey,_Alloc计算值: :常量性,布尔> __gnu_cxx ::的hash_set< _Value,_HashFcn,_EqualKey,_Alloc> ::插入(常量类型名称__gnu_cxx ::哈希表< _Value,_Value,_HashFcn,性病:: _身份< _TP>中_EqualKey,_Alloc> :: VALUE_TYPE&安培;)与_Value = INT *,_HashFcn = __gnu_cxx ::散列< INT *>中_EqualKey =的std :: equal_to< INT *>中_Alloc =的std ::分配器< INT *>]
SRC / ods2.cpp:677:从这里实例化
解决方案
如果您使用的是的std ::阵列< INT,16>
而不是为int *
,所有的问题就会迎刃而解。如果你没有C ++编译器11,可以使用的boost ::阵列
来代替。此外,替换的hash_set
与 unordered_set
。
这似乎没有的std ::哈希
为的std ::阵列
取值专业化,所以我写了我自己的:
的#include< unordered_set>
#包括LT&;阵列GT;空间std
{
模板< typename的T,为size_t N'GT;
结构散列<阵列LT; T,N> >
{
typedef的数组< T,N> argument_type;
的typedef result_type的为size_t; result_type的运算符()(常量argument_type&放大器;一)常量
{
哈希< T>散列器;
result_type的H = 0;
对于(result_type的I = 0; I< N ++ I)
{
H = H * 31 +散列器(A []);
}
返回H;
}
};
}的std :: unordered_set<的std ::阵列< INT,16取代; > closelist;诠释的main()
{
的std ::阵列< INT,16取代; SN = {} 1,2,3,4,5,6,0,8,9,10,11,12,13,14,7,15;
closelist.insert(SN);
}
I need to insert a 1D array into the hashset.
But I got error while compiling.
#include <stdio.h>
#include <stdlib.h>
#include <hash_set.h>
using namespace std;
int hash_comp(const int* state1,const int* state2) {
int result = 0;
for (i = 0; i < 16; i++)
{
if (state1[i] != state2[i]) {
result = -1;
}
}
return result;
}
struct eqArray
{
bool operator()(const int* a1,const int* a2) const
{
return hash_comp(a1,a2) == 0;
}
};
hash_set<int*,hash<int*>,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<int*>, _ExtractKey = std::_Identity<int*>, _EqualKey = std::equal_to<int*>, _Alloc = std::allocator<int*>]':
/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<int*>, _ExtractKey = std::_Identity<int*>, _EqualKey = std::equal_to<int*>, _Alloc = std::allocator<int*>]'
/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<int*>, _ExtractKey = std::_Identity<int*>, _EqualKey = std::equal_to<int*>, _Alloc = std::allocator<int*>]'
/usr/include/c++/4.2.1/ext/hashtable.h:437: instantiated from 'std::pair<__gnu_cxx::_Hashtable_iterator<_Val, _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<int*>, _ExtractKey = std::_Identity<int*>, _EqualKey = std::equal_to<int*>, _Alloc = std::allocator<int*>]'
/usr/include/c++/4.2.1/ext/hash_set:197: instantiated from 'std::pair<typename __gnu_cxx::hashtable<_Value, _Value, _HashFcn, std::_Identity<_Tp>, _EqualKey, _Alloc>::const_iterator, bool> __gnu_cxx::hash_set<_Value, _HashFcn, _EqualKey, _Alloc>::insert(const typename __gnu_cxx::hashtable<_Value, _Value, _HashFcn, std::_Identity<_Tp>, _EqualKey, _Alloc>::value_type&) [with _Value = int*, _HashFcn = __gnu_cxx::hash<int*>, _EqualKey = std::equal_to<int*>, _Alloc = std::allocator<int*>]'
src/ods2.cpp:677: instantiated from here
解决方案
If you use a std::array<int, 16>
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::array
s, so I wrote my own:
#include <unordered_set>
#include <array>
namespace std
{
template<typename T, size_t N>
struct hash<array<T, N> >
{
typedef array<T, N> argument_type;
typedef size_t result_type;
result_type operator()(const argument_type& a) const
{
hash<T> hasher;
result_type h = 0;
for (result_type i = 0; i < N; ++i)
{
h = h * 31 + hasher(a[i]);
}
return h;
}
};
}
std::unordered_set<std::array<int, 16> > closelist;
int main()
{
std::array<int, 16> sn = {1,2,3,4,5,6,0,8,9,10,11,12,13,14,7,15};
closelist.insert(sn);
}
这篇关于C ++如何插入数组哈希集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文