C++ 将哈希函数定义为结构的一部分

C++ 将哈希函数定义为结构的一部分,c++,C++,我知道可以通过定义一个单独的哈希函数struct为struct X定义一个哈希函数: struct hash_X { size_t operator()(const X &x) const {} bool operator()(const X &a, const X &b) const {} }; int main() { unordered_set<X, hash_X, hash_X> s; } 最终目标类似于: struct X

我知道可以通过定义一个单独的哈希函数struct为
struct X
定义一个哈希函数:

struct hash_X {
    size_t operator()(const X &x) const {}
    bool operator()(const X &a, const X &b) const {}
};
int main() {
    unordered_set<X, hash_X, hash_X> s;
}
最终目标类似于:

struct X {
    size_t operator()(void) const {}
    bool operator()(const X &other) const {}
};

int main() {
    unordered_set<X> s;
}
struct X{
size_t运算符()(void)常量{}
布尔运算符()(常量X和其他)常量{}
};
int main(){
无序的集合;
}

这是否可能在C++中?< /p> < P>没有哈希操作,但可以隐藏<代码> Hash < /Calp>Stutt在您的代码> x<代码>:

struct X
{
    std::string name;

    struct hash
    {
        auto operator()( const X& x ) const
        { return std::hash< std::string >()( x.name ); }
    };
};
struct X
{
std::字符串名;
结构散列
{
自动运算符()(常数X和X)常数
{return std::hash()(x.name);}
};
};
你甚至可以让它成为朋友,让它成为私人的,等等


< P>我建议考虑更一般的哈希类。您可以为此类定义可能需要的所有常见哈希操作:

struct ghash {
     // all the values and operations you need here 
}; 
然后,在任何要计算哈希的类中,都可以定义一个转换运算符

struct X { 
    operator ghash () {  // conversion operator 
        ghash rh; 
        // compute the hash
        return rh; 
    }
};
然后,您可以轻松地计算哈希:

 X x; 
 ghash hx = x;   // convert x to a hash 
 hx = (ghash)x;  // or if you prefer to make it visible
这将使扩展散列结构的使用变得更容易,而无需为将来可能需要散列的任何其他结构X、Y、Z重新创建公共基础

现在创建两个容器别名,您不必指定哈希器

要添加新的哈希表,请执行以下操作:

struct Foo {
  std::string s;
  friend size_t hash(Foo const&f){
    return hashing::hasher{}(s);
  }
};
Foo
un\u集合
un\u映射
中工作


我会将对
std
容器和元组的支持添加到
hash
名称空间中(为它们覆盖函数
hash
),神奇的是,它们也会起作用。

std::无序集
std
名称空间中定义。它使用
std::hash
结构来散列许多不同的类型。如果您希望能够使用
std::unordered_set
(不向声明中添加太多信息),则必须创建另一个
std::hash
模板重载,以使其散列您的结构

您应该能够通过执行以下操作使其工作:

# include <unordered_set>
struct X {
    size_t operator()(void) const {}
    bool operator()(const X &other) const {}
};

namespace std {
    template<>
    struct hash<X> {
        inline size_t operator()(const X& x) const {
            // size_t value = your hash computations over x
            return value;
        }
    };
}

int main() {
    std::unordered_set<X> s;
}
struct X {
    ...
    inline bool operator==(const X& other) const {
        // bool comparison = result of comparing 'this' to 'other'
        return comparison;
    }

};
或:

模板
结构等于{
内联布尔运算符()(常量X&a、常量X&b)常量{
//布尔比较=将“a”与“b”进行比较的结果
收益比较;
}
};

没有哈希运算符,但为什么不定义一个方法呢?把它命名为
hash
或其他什么。我希望能够创建一个
无序集
,而不必为hash函数定义额外的结构谢谢。显然,我还必须定义std::equal_,以便在集合中插入X个实例?@icedtrees正式指出!我没有运行代码,因此没有尝试执行任何插入。如果没有比较运算符(或
std::equal_to
模板重载),则无法执行插入。
struct Foo {
  std::string s;
  friend size_t hash(Foo const&f){
    return hashing::hasher{}(s);
  }
};
# include <unordered_set>
struct X {
    size_t operator()(void) const {}
    bool operator()(const X &other) const {}
};

namespace std {
    template<>
    struct hash<X> {
        inline size_t operator()(const X& x) const {
            // size_t value = your hash computations over x
            return value;
        }
    };
}

int main() {
    std::unordered_set<X> s;
}
struct X {
    ...
    inline bool operator==(const X& other) const {
        // bool comparison = result of comparing 'this' to 'other'
        return comparison;
    }

};
template <>
struct equal_to<X> {
    inline bool operator()(const X& a, const X& b) const {
        // bool comparison = result of comparing 'a' to 'b'
        return comparison;
    }
};