相关疑难解决方法(0)

使用自定义类类型作为键的C++ unordered_map

我正在尝试使用自定义类作为关键字unordered_map,如下所示:

#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;

class node;
class Solution;

class Node {
public:
    int a;
    int b; 
    int c;
    Node(){}
    Node(vector<int> v) {
        sort(v.begin(), v.end());
        a = v[0];       
        b = v[1];       
        c = v[2];       
    }

    bool operator==(Node i) {
        if ( i.a==this->a && i.b==this->b &&i.c==this->c ) {
            return true;
        } else {
            return false;
        }
    }
};

int main() {
    unordered_map<Node, int> m;    

    vector<int> v;
    v.push_back(3);
    v.push_back(8);
    v.push_back(9);
    Node n(v);

    m[n] = 0; …
Run Code Online (Sandbox Code Playgroud)

c++ hash unordered-map g++ hashtree

258
推荐指数
3
解决办法
22万
查看次数

boost :: hash_combine中的幻数

所述boost::hash_combine模板函数采用一个散列(称为参考seed)和对象v.根据文档,它结合seedvby 的哈希

seed ^= hash_value(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
Run Code Online (Sandbox Code Playgroud)

我可以看出这是确定性的.我明白为什么要使用XOR.

我敢打赌,这个加法有助于将相似的值广泛分开,因此探测哈希表不会崩溃,但有人可以解释这个神奇常数是什么吗?

c++ algorithm hash boost magic-numbers

86
推荐指数
2
解决办法
2万
查看次数

如何在C++中创建一组无序的整数对?

以下程序不会编译一组无序的整数对,但它会对整数进行编译.Can unordered_set和它的成员函数可以用在用户定义的类型上,我该如何定义它?

#include <unordered_set>
...

class A{
...
private: 
    std::unordered_set< std::pair<int, int> > u_edge_;
};
Run Code Online (Sandbox Code Playgroud)

编译错误:

错误:没有匹配函数来调用'std :: unordered_set> :: unordered_set()'

c++ unordered-set std-pair

42
推荐指数
6
解决办法
3万
查看次数

如何将unordered_set与自定义结构一起使用?

我想使用unordered_set带有自定义的struct。在我的情况下,自定义struct表示欧氏平面中的2D点。我知道应该定义一个哈希函数和比较器运算符,并且我已经做到了,如下面的代码所示:

struct Point {
    int X;
    int Y;

    Point() : X(0), Y(0) {};
    Point(const int& x, const int& y) : X(x), Y(y) {};
    Point(const IPoint& other){
        X = other.X;
        Y = other.Y;
    };

    Point& operator=(const Point& other) {
        X = other.X;
        Y = other.Y;
        return *this;
    };

    bool operator==(const Point& other) {
        if (X == other.X && Y == other.Y)
            return true;
        return false;
    };

    bool operator<(const Point& other) {
        if (X < other.X …
Run Code Online (Sandbox Code Playgroud)

c++ struct set unordered-set c++11

5
推荐指数
3
解决办法
2492
查看次数