Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何为std::vector编写哈希函数<;标准::向量<;布尔>&燃气轮机;_C++_Hash_Struct_Stl_Unordered Set - Fatal编程技术网

C++ 如何为std::vector编写哈希函数<;标准::向量<;布尔>&燃气轮机;

C++ 如何为std::vector编写哈希函数<;标准::向量<;布尔>&燃气轮机;,c++,hash,struct,stl,unordered-set,C++,Hash,Struct,Stl,Unordered Set,我有一个结构,它有一个变量,一个表示网格的std::vector。如果轴网相等,或者轴网的任何旋转相等,则其中一个结构与另一个结构相等。我正试图使用一个无序集来存储其中的许多,然而,经过一些研究,我发现我需要某种哈希函数。我以前从未使用过哈希函数,我在引用它时发现的东西让我感到困惑。那么,我的问题是,如何/什么是为这种数据类型编写哈希函数的最佳方法,还是只使用无序的网格集并在添加它们时测试旋转更好 一些代码: intnx,纽约; 向量网格; 结构旋转网格{ 公众: 网格数据; rotateab

我有一个结构,它有一个变量,一个表示网格的
std::vector
。如果轴网相等,或者轴网的任何旋转相等,则其中一个结构与另一个结构相等。我正试图使用一个无序集来存储其中的许多,然而,经过一些研究,我发现我需要某种哈希函数。我以前从未使用过哈希函数,我在引用它时发现的东西让我感到困惑。那么,我的问题是,如何/什么是为这种数据类型编写哈希函数的最佳方法,还是只使用无序的网格集并在添加它们时测试旋转更好

一些代码:

intnx,纽约;
向量网格;
结构旋转网格{
公众:
网格数据;
rotateableGrid(网格数据):数据(数据){}
rotateableGrid(rotateableGrid&rg):数据(rg.data){}
布尔运算符==(常量rotateableGrid和rhs){
对于(int c=0;c<4;c++){
如果(旋转(c)=rhs.数据)返回true;
}
返回false;
}
私人:
栅格旋转(整数金额){
如果(金额%4==0)返回数据;
网格ret(ny,std::vector(nx));
对于(int x=0;x=0)ret[x][nx-1-y]=data[y][x];
打破
案例2:
如果(nx-1-x>=0&&ny-1-y>=0)ret[ny-1-y][nx-1-x]=data[y][x];
打破
案例3:
如果(ny-1-x>=0&&y
提前谢谢


<强>注:< /强>我在VS 2013

< P>中使用C++,你可以做的是把矩阵中所有向量的散列组合起来。对于
std::vector
,存在过载。如果你尝试这样的东西

size_t hash_vector(const std::vector< std::vector<bool> >& in, size_t seed)
{
    size_t size = in.size();
    std::hash< std::vector<bool> > hasher;
    for (size_t i = 0; i < size; i++)
    {
        //Combine the hash of the current vector with the hashes of the previous ones
        seed ^= hasher(in[i]) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
    }
    return seed;
}
但是,由于
rotateableGrid
的rotate成员标记为private,因此您必须将
hash_grid
声明为
rotateableGrid
的朋友。为此,您必须在
rotateableGrid

friend size_t hash_grid(rotateableGrid&, size_t);

除此之外,该方法不是旋转不变的,正如所讨论的那样。处理这个问题的最简单方法是将所有四个旋转的哈希值与一些交换操作(加法或异或)结合起来。
size_t hash_grid(rotateableGrid& in, size_t seed = 92821)
             //  ^^^^^ Should be const, but rotate isn't marked const
{
    return hash_vector(in.data) ^ hash_vector(in.rotate(1).data) ^ hash_vector(in.rotate(2).data) ^ hash_vector(in.rotate(3).data);
}
friend size_t hash_grid(rotateableGrid&, size_t);