Guest User

Untitled

a guest
Apr 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. if(message1 > message2)
  2. { ... }
  3.  
  4. if(message1 < message2)
  5. { ... }
  6.  
  7. friend bool operator>(const Message& m1, const Message& m2)
  8.  
  9. struct RealFraction {
  10. RealFraction(int x) { this.num = x; this.den = 1; }
  11. RealFraction(int num, int den) { normalize(num, den); }
  12. // Rest of code omitted.
  13.  
  14. bool operator <(RealFraction const& rhs) {
  15. return num * rhs.den < den * rhs.num;
  16. }
  17. };
  18.  
  19. int x = 1;
  20. RealFraction y = 2;
  21. if (y < x) …
  22.  
  23. if (x < y) …
  24.  
  25. inline operator>(const Message& lhs, const Message& rhs)
  26. {
  27. return rhs < lhs;
  28. }
  29.  
  30. inline operator<=(const Message& lhs, const Message& rhs)
  31. {
  32. return !(rhs < lhs);
  33. }
  34.  
  35. inline operator>=(const Message& lhs, const Message& rhs)
  36. {
  37. return !(lhs < rhs);
  38. }
  39.  
  40. #include <queue>
  41. #include <string>
  42. #include <functional>
  43. #include <vector>
  44.  
  45. class Message
  46. {
  47. int priority;
  48. std::string contents;
  49. //...
  50. public:
  51. Message(int priority, const std::string msg):
  52. priority(priority),
  53. contents(msg)
  54. {}
  55. int get_priority() const { return priority; }
  56. //...
  57. };
  58.  
  59. struct ComparePriority:
  60. std::binary_function<Message, Message, bool> //this is just to be nice
  61. {
  62. bool operator()(const Message& a, const Message& b) const
  63. {
  64. return a.get_priority() < b.get_priority();
  65. }
  66. };
  67.  
  68. int main()
  69. {
  70. typedef std::priority_queue<Message, std::vector<Message>, ComparePriority> MessageQueue;
  71. MessageQueue my_messages;
  72. my_messages.push(Message(10, "Come at once"));
  73. }
  74.  
  75. class MessageQueue
  76. {
  77. std::vector<Message> messages;
  78. ComparePriority compare;
  79. //...
  80. void push(const Message& msg)
  81. {
  82. //...
  83. if (compare(msg, messages[x])) //msg has lower priority
  84. //...
  85. }
  86. };
Add Comment
Please, Sign In to add comment