I have a struct "Telegram" which contains a time offset, the Sender ID, the Receiver ID, and the Message itself:

struct Telegram
{
    int Msg; // Message 
    unsigned long Time; // Time offset (i.e., GetTicks() etc.)
    int Sender; // ID of Sender
    int Receiver; // ID of Receiver

    Telegram(int tMsg, unsigned long tTime, int tSender, int tReceiver):Msg(tMsg), Time(tTime), Sender(tSender), Receiver(tReceiver) {}

    // operator override to sort Telegrams by Time.
    bool operator<(const Telegram& t1) const
    {
        return Time < t1.Time;
    }
};

I want to store these in an STL set, so that those inserted with low Time offsets appear at the beginning and those with later Time offsets appear towards the back of the set.

So if a duplicate Telegram appears
Example 1:

Telegram(MSG, 50, 1, 2) // MSG, 50 msecs, from ID 1 to ID 2
Telegram(MSG, 50, 1, 2) // MSG, 50 msecs, from ID 1 to ID 2 *IGNORED*

Only one makes it's way into the set.

However... because the set is dependent on the sorting algorithm, the second valid telegram in the next example is lost (all because they have the same Time offset, the STL set assumes they are identical, even though they are going to different people with different messages)

Example 2:

// Same time offset, but between 2 different IDs this time.
Telegram(MSG_CHEESY, 50, 1, 2) // MSG, 50 msecs, from ID 1 to ID 2
Telegram(MSG_CHALKY, 50, 15, 32) // MSG, 50 msecs, from ID 15 to ID 22 *IGNORED - UNFORTUNATELY! :(*

I don't want to use the "multiset", because then I'll get duplicates as seen in Example 1.

Do I need to implement a Pair as a sorting Key here (to allow duplicate Times if the Telegram contents are different)?

In addition, I found that changing the sorting criterea works... but it's awful... and probably not the way a respectable programmer would solve the problem. :'(

bool operator<(const Telegram& t1) const
    {
        if(Time==t1.Time)
        { // Same Time?
            if(Receiver==t1.Receiver)
            { // Same Receiver?
                if(Sender==t1.Sender)
                { // Same Sender?
                    if(Msg==t1.Msg)
                    { // Same Message?
                        return false; // Bah... EPIC FAIL, exact duplicate.
                    }
                    else return Msg < t1.Msg; // sub-Sort by Msg
                }
                else return Sender < t1.Sender; // sub-Sort by Sender
            }
            else return Receiver < t1.Receiver; // sub-Sort by Receiver
        }
        else
            return Time < t1.Time; // Sort by Time (default)
    }

This works (and looks nicer). :) But if anyone knows the 'correct' way to solve this problem (I'm probably using the wrong container), then I would be very grateful.

bool operator<(const Telegram& t1) const
{
   if(Time==t1.Time) // Same time? Then sort by Telegram characteristics.
      return (Receiver < t1.Receiver) || (Sender < t1.Sender) || (Msg < t1.Msg);
    return (Time < t1.Time); // Otherwise, earliest Telegram first. 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.