How can I make an unordered set of pairs of integers in C++?

前端 未结 8 1458
陌清茗
陌清茗 2020-12-08 00:16

The following program does not compile an unordered set of pairs of integers, but it does for integers. Can unordered_set and its member functions be used on us

8条回答
  •  抹茶落季
    2020-12-08 00:45

    As already mentioned in most of the other answers on this question, you need to provide a hash function for std::pair. However, since C++11, you can also use a lambda expression instead of defining a hash function. The following code takes the solution given by dasblinkenlight as basis:

    auto hash = [](const std::pair& p){ return p.first * 31 + p.second; };
    std::unordered_set, decltype(hash)> u_edge_(8, hash);
    

    Code on Ideone

    I'd like repeat dasblinkenlight's disclaimer: This solution is limited to a pair of two integers. This answer provides the idea for a more general solution.

提交回复
热议问题