mardi 26 décembre 2017

how to write a customize hash function for unordered set which defined in the customize structure?

I'm trying to write a new hash function for my own structure, but it seems I entered to the dead end and don't know how to proceed..Here is my code: I have a structure named people:

struct people{
    int id;
    unordered_set<people>friends;
    people(int x) : id(x) {}
};

And I need a hash function for the friends, like:

struct peopleHash{
    size_t operator()(const people& p) const{
        return p.id;
    }
}

And here comes the problem ,if I initialize people first, the peopleHash function would find "undefined type people", same if I initialize peopleHash first. I tried to define peopleHash inside of the people struct, like:

struct people{
    int id;
    struct peopleHash{
        size_t operator()(const people& p) const{
            return p.id;
        }
    };
    unordered_set<people, peopleHash>friends;
    people(int x) : id(x) {}
};

But the compiler said: error: invalid operands to binary expression ('const people' and 'const people') {return __x == __y;} I have no idea how to deal with this...Any help?

Aucun commentaire:

Enregistrer un commentaire