We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<iostream>#include<algorithm>#include<vector>usingnamespacestd;classMessage{stringmessage;intorder;staticinttot_n;public:Message(){tot_n++;order=tot_n;}Message(conststring&s){tot_n++;order=tot_n;message=s;}conststring&get_text(){returnmessage;}booloperator<(constMessage&m){return(order<m.order);}};intMessage::tot_n=0;classMessageFactory{public:MessageFactory(){}Messagecreate_message(conststring&text){Messagem(text);returnm;}};classRecipient{public:Recipient(){}voidreceive(constMessage&msg){messages_.push_back(msg);}voidprint_messages(){fix_order();for(auto&msg:messages_){cout<<msg.get_text()<<endl;}messages_.clear();}private:voidfix_order(){sort(messages_.begin(),messages_.end());}vector<Message>messages_;};classNetwork{public:staticvoidsend_messages(vector<Message>messages,Recipient&recipient){// simulates the unpredictable network, where sent messages might arrive in unspecified orderrandom_shuffle(messages.begin(),messages.end());for(automsg:messages){recipient.receive(msg);}}};intmain(){MessageFactorymessage_factory;Recipientrecipient;vector<Message>messages;stringtext;while(getline(cin,text)){messages.push_back(message_factory.create_message(text));}Network::send_messages(messages,recipient);recipient.print_messages();}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Messages Order
You are viewing a single comment's thread. Return to all comments →
use c++ version 2014 otherwise not work