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.
- Prepare
- C++
- Debugging
- Messages Order
- Discussions
Messages Order
Messages Order
+ 0 comments 1) I don't understand the use of < operator. Do we have to perform operator overloading? how is it being used in the sort() 2) How can we restore the original order by sorting? wouldn't sorting work for alphabetically arranging or smth like that?
+ 0 comments Here is my solution. I use a static counter for each message created and I attribute it a rank. These ranks are used in the operator< to retrieve the messages order.
class Message { private: string text; static int nbMessages; int rank; public: Message() {} Message(const string& ptext): text(ptext) { nbMessages++; rank = nbMessages; }; const string& get_text() { return text; } const int& get_rank() { return rank; } bool operator< (Message&); }; bool Message::operator<(Message& msg){ if(rank <= msg.get_rank()) return true; return false; } int Message::nbMessages = 0; class MessageFactory { public: MessageFactory() {} Message create_message(const string& text) { Message msg(text); return msg; } };
+ 0 comments Here are the solution of Messages Order in C++ Hacker Rank Solution
+ 0 comments Here are the solution of Messages Order in C++ HackerRank Solution https://www.brokenprogrammers.com/messages-order-in-cpp-hackerrank-solution/
+ 0 comments class Message { int currId; string text; public: Message() : currId{0}, text{} {} Message(int id, const string &msgText) : currId{id}, text{msgText} {} const string& get_text() const { return text; } friend bool operator<(const Message &lhs, const Message &rhs) { return lhs.currId < rhs.currId; } }; class MessageFactory { int msgId = 0; public: MessageFactory() {} Message create_message(const string& text) { return Message{msgId++, text}; } };
Load more conversations
Sort 78 Discussions, By:
Please Login in order to post a comment