Jerry Coffin's suggestion is a nice one, it is extensible to any number of keys of arbitrary width.
However, in practice, there are many situations where the following efficient method can be employed. If the size of the keys are such that their sum fits into a native type, then we can compose the key as following.
Suppose we have three key-parts:
a, 16-bit,
b, 32-bit
c, 16-bit
listed in the order of significance for comparison. (Same as in example of Jerry Coffin.)
Then, we can have one value
class key {
private:
uint64_t key_rep_; // internal key representation
};
with the following underlying interpretation of key_rep_
AAAABBBBBBBBCCCC
Each letter above is a nibble, and shows the key-part it represents.
Comparing this approach with the straightforward one:
- reading and writing key-part into the key are slower
- comparing two keys are faster
In a map or set, comparison of keys are much more frequent than read/write, so this approach, when applicable, achieves overall efficiency.