c++如何将数组插入哈希集
C++ how to insert array into hash set?
我需要在哈希集中插入一个1D数组。
但是在编译时出现了错误。
#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
如果你使用std::array<int, 16>
而不是int*
,你所有的问题都会消失。如果你没有c++ 11编译器,你可以使用boost::array
代替。同时,将hash_set
替换为unordered_set
。
似乎没有std::hash
对std::array
s的专门化,所以我自己写了:
#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);
}
您没有发布实际的错误消息,只发布了跟踪。也就是说,这可能是因为你的数据集使用非const
int*
,而你的数据是const
。
也就是说,我建议使用unordered_set
而不是hash_set
(无论是从std::tr1
,如果你的编译器支持,或从std
,如果你的编译器支持c++ 11或从Boost),并听取弗雷德的建议,使用{std,boost}::array
而不是原始指针。
我认为不存在专门的hash<int*>
。我添加了这个:
namespace __gnu_cxx{ //I'm not sure what compiler version you used,
//mine wanted this
template<>
struct hash<const int*>{
size_t operator()(const int*a) const{
size_t r = 0;
for (int i=0;i<16;i++)
r = (r<<1) ^ a[i]; //not sure if it makes sense as a hash.
return r;
}
};
}
我还放了一些const
s,它编译了
相关文章:
- 在具有开放寻址的哈希表中插入节点 [优化逻辑]
- 如何为位集找到/实现一个好的哈希函数
- 如果索引不是整数,我们如何在 C++ 中插入哈希表
- 在 C++ 中为特定哈希表创建插入函数
- 插入不从哈希函数中检索键
- 通过线性探测插入哈希表
- 如果存在哈希,请加快C unordered_map插入
- 使用按插入方式排序的参数创建哈希映射
- 哈希表打印和插入功能
- 确定元素是否已插入到哈希映射中
- 哈希集、哈希映射和哈希表
- 将新元素插入哈希表
- 插入哈希表
- 用于哈希的哈希集函数
- c++如何将数组插入哈希集
- 哈希集搜索函数不工作c++
- 如何使用模板类的模板类子类从哈希表实现哈希集
- 将typedef map插入哈希表
- 如何将C++ "int"添加到哈希集<Integer>?
- 正在将对象插入哈希表...出于某种原因不让我?