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.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. C++
  3. Debugging
  4. Messages Order
  5. Discussions

Messages Order

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 78 Discussions, By:

recency

Please Login in order to post a comment

  • mh07599
    2 months ago+ 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?

    1|
    Permalink
  • anoh_erwin
    3 months ago+ 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|
    Permalink
  • aadinew7
    4 months ago+ 0 comments

    Here are the solution of Messages Order in C++ Hacker Rank Solution

    0|
    Permalink
  • brokenprogramme1
    4 months ago+ 0 comments

    Here are the solution of Messages Order in C++ HackerRank Solution https://www.brokenprogrammers.com/messages-order-in-cpp-hackerrank-solution/

    0|
    Permalink
  • petes40
    4 months ago+ 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};
        }
    };
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy