fork download
  1. #include <iostream>
  2. #include <map>
  3.  
  4. struct MyKey {
  5.  
  6. // key data
  7. std::string addr;
  8. int priority;
  9.  
  10. // constructor
  11. MyKey(const std::string & s, const int p)
  12. : addr(s), priority(p) {}
  13.  
  14. // overloaded operator
  15. bool operator<(const MyKey &that) const {
  16.  
  17. // same key if addr is the same
  18. if (that.addr == this->addr)
  19. return false;
  20.  
  21. // not same key so look at priorities to determine order
  22. if (that.priority < this->priority)
  23. return true;
  24. if (that.priority > this->priority)
  25. return false;
  26.  
  27. // priorities are the same so use the string compare
  28. return (that.addr > this->addr);
  29. }
  30. };
  31.  
  32.  
  33. int main()
  34. {
  35. MyKey mk1{ "a",3 }, mk2{ "b", 2 }, mk3 { "a", 1 };
  36. std::cout << (mk1 < mk2) << std::endl;
  37. std::cout << (mk2 < mk3) << std::endl;
  38. std::cout << (mk1 < mk3) << std::endl;
  39. }
Success #stdin #stdout 0s 4260KB
stdin
Standard input is empty
stdout
1
1
0