fork download
  1. #include <set>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. template<class T>
  6. class A
  7. {
  8. public:
  9. A(T a = T(), T b = T()): m_a(a), m_b(b) {}
  10.  
  11. bool operator<(const A& rhs) const
  12. {
  13. return ((m_a < rhs.m_a) ||
  14. ((m_a == rhs.m_a) && (m_b < rhs.m_b))
  15. );
  16. /* alterrnatively:
  17.   return std::tie(m_a, m_b) < std::tie(rhs.m_a, rhs.m_b);
  18.   */
  19. }
  20.  
  21. friend ostream& operator<<(ostream &out, const A& value)
  22. {
  23. return out << value.m_a << ' ' << value.m_b;
  24. }
  25.  
  26. private:
  27. T m_a;
  28. T m_b;
  29. };
  30.  
  31. int main()
  32. {
  33. A<int> abc(2,3);
  34. A<int> def(1,5);
  35.  
  36. set<A<int>> P2D;
  37. P2D.insert(abc);
  38. P2D.insert(def);
  39.  
  40. for(const auto &elem : P2D) {
  41. cout << elem << endl;
  42. }
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0.01s 5516KB
stdin
Standard input is empty
stdout
1 5
2 3