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 the sorting on member vars a, b, c, d, e, the only thing I want to ensure is that the map is sorted on timestamp. I also just realized that if I have two instances of KeyT one, two, where everything is the same except for “d”, then both one < two, and two < one would be false. The only way to fix that is to write comparisons for all possible combinations of all member variables.. I’m fairly certain I’m missing something obvious, so what’s the best solution in this case?
Thanks!
I think this does what you need:
This is a sensible if ugly pattern to use whenever you have a prioritised list of variables you want to sort on within a comparable class.