Unordered set of pairs, compilation error

前端 未结 1 1140
孤街浪徒
孤街浪徒 2020-12-01 23:40

I am trying to create an unordered set of pairs

So far I have :

typedef std::pair Move;
typedef std::unordered_set Set;
<         


        
相关标签:
1条回答
  • 2020-12-02 00:34

    That error message appears when you have failed to specialise std::hash or provide a hasher type for your unordered container (see e.g. Using C++11 unordered_set in Visual C++ and clang). The XCode error in this case is particularly unfriendly!

    C++11 does not provide a hash for pairs (or tuples), even of hashable types. This discussion indicates that this was primarily due to a lack of time to get anything better in; however I'm not aware whether there will be anything better in C++14.

    Specialising std::hash<std::pair<int, int>> is probably not a good idea (and is not permitted by the language; specialising std templates is only allowed for user-defined types), so you'll have to provide a hasher:

    struct MoveHasher {
        std::size_t operator()(const std::pair<int, int> &val) const { ... }
    };
    typedef std::unordered_set<Move, MoveHasher> Set;
    

    See How do I combine hash values in C++0x? for how to write the hash function.

    Alternatively, you could make Move a user-defined class (probably a good idea!) and then specialising std::hash will be fine.

    0 讨论(0)
提交回复
热议问题