Say I have some POD struct Foo
in C++ which I want to put into an ordered container like set<Foo>
. I do not care about which ordering is used, only that it be consistent and fast. What is the best way to do this?
More concretely, say I am on a 64-bit machine with sizeof(long)==8
. Say the class is:
struct Foo{
long key; //8 bytes : 8 total
int fum; // 4 bytes : 12 total
short n; // 2 bytes : 14 total
short n[3]; //6 bytes : 20 total
char bar[5]; // 5 bytes : 25 total
char name[23]; //22 bytes : 47 total
char padding[1]; // 1 byte : 48 total
}
the padding
field is just to make sure the size is multiple of 8.
Note that sizeof(Foo)
is 48 so its contents can be represented in 6 unsigned long
s, which is obviously the best way to do comparisons, rather than walking through each field.
Can the compiler do this for me automatically? Or do I have to either define a union
with a field unsigned long data[8]
, or cast a Foo *
to unsigned long *
in my operator<
method for Foo
?
Note that if there is no padding field, then one must generate 5 unsigned long
comparisons, followed by comparisons of a long
, a short
, and a char
comparison. This whole thing is kind of messy.
This issue must come up all the time, so I am guessing there is some best/best way to handle it.
If you want to compare using the struct actual value, it's best to define an operator<
that has a bit more logic and gives some kind of logical ordering between the fields.
If you just want the objects sortable for fast access, you can just hold the pointes. Those are just numbers, and are sortable.
If you want to use the actual struct data, but don't care about any kind of ordering between the members, you can just use memcmp with sizeof(Foo)
.