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

前端 未结 8 1478
陌清茗
陌清茗 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:47

    OK here is a simple solution with guaranteed non collisions. Simply reduce your problem to an existing solution i.e. convert your pair of int to string like so:

     auto stringify = [](const pair& p, string sep = "-")-> string{
        return to_string(p.first) + sep + to_string(p.second);
     }
    
     unordered_set myset;
     myset.insert(stringify(make_pair(1, 2)));
     myset.insert(stringify(make_pair(3, 4)));
     myset.insert(stringify(make_pair(5, 6)));
    

    Enjoy!

提交回复
热议问题