fork(1) download
  1. #include <iostream>
  2. #include <set>
  3.  
  4. typedef std::pair<int, int> PairInt;
  5.  
  6. namespace std
  7. {
  8. template<>
  9. bool operator < (const PairInt& l, const PairInt& r)
  10. {
  11. //swap only if they're unequal to avoid infinite recursion
  12. if (l.first != l.second)
  13. {
  14. //swap elements, considering your special case
  15. if (l.first == r.second && l.second == r.first)
  16. return l < PairInt(r.second, r.first); //call again!
  17. }
  18.  
  19. //actual comparison is done here
  20. if ( l.first != r.first )
  21. return l.first < r.first;
  22. else
  23. return l.second < r.second;
  24. }
  25. }
  26.  
  27. int main()
  28. {
  29. std::set<PairInt> intSet;
  30. intSet.insert(PairInt(1,3));
  31. intSet.insert(PairInt(1,4));
  32. intSet.insert(PairInt(1,4));
  33. intSet.insert(PairInt(4,1));
  34. for(auto it = intSet.begin(); it != intSet.end(); ++it)
  35. std::cout << it->first << "," << it->second << std::endl;
  36. }
Success #stdin #stdout 0s 2964KB
stdin
Standard input is empty
stdout
1,3
1,4