- Prepare
- C++
- Debugging
- Messages Order
- Discussions
Messages Order
Messages Order
+ 0 comments class Message { string message; int order; static int tot_n; public: Message(){ tot_n++; order = tot_n; } Message(const string& s){ tot_n++; order = tot_n; message = s; } const string& get_text() { return message; } bool operator<(const Message& m){ return (order < m.order); } }; int Message::tot_n = 0; class MessageFactory { public: MessageFactory() {} Message create_message(const string& text) { Message m(text); return m; } };
+ 0 comments include
include
include
using namespace std;
class Message { private: string text; static int id; int current_id; public: Message() { current_id = ++id; } Message(string t){ current_id = ++id; text=t; } const string& get_text() { return text; } // overloaded < operator bool operator < (const Message& M2) { if(current_id < M2.current_id) return true; else return false; } }; int Message::id = 0;
class MessageFactory { public: MessageFactory() {} Message create_message(const string& text) { Message m = Message(text); return m; } };
class Recipient { public: Recipient() {} void receive(const Message& msg) { messages_.push_back(msg); } void print_messages() { fix_order(); for (auto& msg : messages_) { cout << msg.get_text() << endl; } messages_.clear(); } private: void fix_order() { sort(messages_.begin(), messages_.end()); } vector messages_; };
class Network { public: static void send_messages(vector messages, Recipient& recipient) { // simulates the unpredictable network, where sent messages might arrive in unspecified order random_shuffle(messages.begin(), messages.end());
for (auto msg : messages) { recipient.receive(msg); } } };int main() { MessageFactory message_factory; Recipient recipient; vector messages; string text; while (getline(cin, text)) { messages.push_back(message_factory.create_message(text)); } Network::send_messages(messages, recipient); recipient.print_messages(); }
+ 0 comments Most of us will use two Message members for the text and some sort of id, I guess.
Little caveat: don't declare them const !
The locked code uses 'sort', which calls 'move', and the later requires all members to be non-const (else you get some long and cryptic complaint from the compiler).
+ 0 comments I think my approach is quite similar to the others already published here. It involves the usage of a static variable.
- I added a static variable "
order
", class-wise. - In addition, each instance of Message also contains its own copy of order, called
order_
. - On creation time,
Message()
callsset_order()
which increments the globalorder
by one, then copies that value to the interval variableorder_
. For that, theMessage
's constructor callsset_order()
. - Messages are sorted by
order_
.
class Message { public: Message() { // this will set the order or arrival, on creation time set_order(); } Message(const string text): Message() {text_=text;} const string& get_text() { return text_; } // global variable to keep count of the number of messages created static int order; // public getter function int get_order() const { return order_; } // comparison operator bool operator<(const Message& other) const { return order_ < other.get_order(); } private: string text_; // this is for internal use int order_; // this is called on creation time: set the order of arrival for the message void set_order() { order_ = ++Message::order; } }; int Message::order = 0; class MessageFactory { public: MessageFactory() {} Message create_message(const string& text) { return Message(text); } };
- I added a static variable "
+ 1 comment class Message { private: string s; int order; public: Message(const string& text, int Order) { s = text; order = Order; } const string& get_text() { return s; } bool operator < (const Message& second) { return order < second.order; } }; class MessageFactory { public: static int counter; MessageFactory() {} Message create_message(const string& text) { Message temp(text, counter); counter++; return temp; } }; int MessageFactory::counter = 1;
Sort 88 Discussions, By:
Please Login in order to post a comment