Std :: map, custom key type with sorting by only one variable

I have a key type:

struct KeyT {
    uint32_t timestamp;

    // example!
    uint32_t a;
    uint32_t b;
    uint32_t c;
    uint32_t d;
    uint32_t e;
    // ...

    bool operator== (const KeyT& key) const
    {
        if(timestamp == key.timestamp && a == key.a && b == key.b && d == key.d && c == key.c && e == key.e)
            return true;
        return false;

    }
    bool operator< (const KeyT& key) const
    {
        if(timestamp < key.timestamp)
            return true;
        else if(timestamp == key.timestamp && a < key.a && b < key.b && c < key.c && d < key.d && e < key.e)
            return true;
        else if(timestamp == key.timestamp && a == key.a && b < key.b && c < key.c && d < key.d && e < key.e)
            return true;
        else if(timestamp == key.timestamp && a == key.a && b == key.b && c < key.c && d < key.d && e < key.e)
            return true;
        else if(timestamp == key.timestamp && a == key.a && b == key.b && c == key.c && d < key.d && e < key.e)
            return true;
        else if(timestamp == key.timestamp && a == key.a && b == key.b && c == key.c && d == key.d && e < key.e)
            return true;
        // ..
        return false;
    }
};

Now, I don't really care about sorting on members a, b, c, d, e, the only thing I want to guarantee is that the map is sorted by timestamp. I also just realized that if I have two instances of KeyT one, two, where everything is the same, except for "d", then one <two and two one would be false. The only way to fix this is to write comparisons for all possible combinations of all member variables. I am sure that I am missing something obvious, and what is the best solution in this case?

Thanks!

+1
source share
3 answers

, , :

bool operator< (const KeyT& key) const
{
    if(timestamp != key.timestamp) return timestamp < key.timestamp;
    else if ( a != key.a ) return a < key.a;
    else if ( b != key.b ) return b < key.b;
    else if ( c != key.c ) return c < key.c;
    else if ( d != key.d ) return d < key.d;
    else return e < key.e;
}

, , , .

+6

-, , , , , else return, :

bool operator< (const KeyT& key) const
{
    if(timestamp < key.timestamp)
        return true;
    if(timestamp == key.timestamp && a < key.a)
        return true;
    if(timestamp == key.timestamp && a == key.a && b < key.b)
        return true;
    if(timestamp == key.timestamp && a == key.a && b == key.b && c < key.c)
        return true;
    if(timestamp == key.timestamp && a == key.a && b == key.b && c == key.c && d < key.d)
        return true;
    if(timestamp == key.timestamp && a == key.a && b == key.b && c == key.c && d == key.d && e < key.e)
        return true;
    // ..
    return false;
}

, if :

bool operator< (const KeyT& key) const
{
    return timestamp < key.timestamp;
}

, .

+1

If all you care about is a timestamp and the other attributes just don't matter, you can use the following, which is a strict weak order. You will never know in what order the objects of the same timestamp will appear:

bool operator< (const KeyT& key) const
{
    return timestamp < key.timestamp;
}

If you need to order all the attributes, I suggest boost::tie:

bool operator< (const KeyT& key) const
{
    return boost::tie(timestamp, a, b, c, d, e) < boost::tie(key.timestamp, key.a, key.b, key.c, key.d, e.key);
}
0
source

Source: https://habr.com/ru/post/1526314/


All Articles