Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if(message1 > message2)
- { ... }
- if(message1 < message2)
- { ... }
- friend bool operator>(const Message& m1, const Message& m2)
- struct RealFraction {
- RealFraction(int x) { this.num = x; this.den = 1; }
- RealFraction(int num, int den) { normalize(num, den); }
- // Rest of code omitted.
- bool operator <(RealFraction const& rhs) {
- return num * rhs.den < den * rhs.num;
- }
- };
- int x = 1;
- RealFraction y = 2;
- if (y < x) …
- if (x < y) …
- inline operator>(const Message& lhs, const Message& rhs)
- {
- return rhs < lhs;
- }
- inline operator<=(const Message& lhs, const Message& rhs)
- {
- return !(rhs < lhs);
- }
- inline operator>=(const Message& lhs, const Message& rhs)
- {
- return !(lhs < rhs);
- }
- #include <queue>
- #include <string>
- #include <functional>
- #include <vector>
- class Message
- {
- int priority;
- std::string contents;
- //...
- public:
- Message(int priority, const std::string msg):
- priority(priority),
- contents(msg)
- {}
- int get_priority() const { return priority; }
- //...
- };
- struct ComparePriority:
- std::binary_function<Message, Message, bool> //this is just to be nice
- {
- bool operator()(const Message& a, const Message& b) const
- {
- return a.get_priority() < b.get_priority();
- }
- };
- int main()
- {
- typedef std::priority_queue<Message, std::vector<Message>, ComparePriority> MessageQueue;
- MessageQueue my_messages;
- my_messages.push(Message(10, "Come at once"));
- }
- class MessageQueue
- {
- std::vector<Message> messages;
- ComparePriority compare;
- //...
- void push(const Message& msg)
- {
- //...
- if (compare(msg, messages[x])) //msg has lower priority
- //...
- }
- };
Add Comment
Please, Sign In to add comment