I have some opaque bytes which I want to use in an std::map, both as keys and values. I created a class, OpaqueBytes, which has two (private) members: _data and _size, to store the bytes. My Cocoa background tells me that I would need to implement a hashing function and equality function on the class to use it in a std::map. Problem is, it seems like the STL requires strict weak ordering, based on Can a STL map be used with keys of varying sizes.
I have several questions:
-
Is my intuition to create a class to represent opaque bytes correct? Is there a class in the STL which already exists to hold opaque bytes? In Cocoa parlance, is there an equivilent to
NSData? -
How should I implement the ordering of
OpaqueBytes? I can’t just compare the bytes usingmemcmp, because the two byte strings could be of differing length – I wouldn’t want FF EE to be considered equal to FF.
If you need to use OpaqueBytes as a key you will need a strict comparison. This can be a member function or a non-member function.
Could use something like this:
You could compare size first, of course. You’d get a different ordering but it would still be strict.
(I still think, as an aside, that map would be better implemented with a compare function than a less).