I have a general question. Let's assume I have a C++ class with multiple fields of different types. I want/need to store the objects of this class in std::set
or std::map
(in order to access them in O(log(N)).
In order to do it I need to overload operator<
BUT what if operator<
has no any logical meaning in my case? For example I have class faceDescription
which contains fields like eye color, nose type etc.
The most obvious would be to implement operator<
just by comparing each field like this:
if (fieldA < other.fieldA)
{
return true;
}
else if (fieldA == other.fieldA && fieldB < other.fieldB)
...
and so on. But if I have many fields this method will be too long with too many branches, hardly readable and probably hard to maintain.
I was thinking about "packing" all the fields into a buffer and then compare it with something like std::memcmp
but the point is that some fields may be pointers or different classes/structs.
So my question:
Is there an efficient and generic way to define a "unique identifier" to the class (maybe with some std
methods) based on the fields values so that this "unique identifier" could be used to compare/sort objects of that class?
EDIT
Just an example which may explain the motivation and should be clear for everyone:
Assume video processing with face recognition so that the program receives face description object and it has to count how many times each face appeared during the given video. There may be thousands/millions of faces. So the efficient way to do it is to maintain a map of face description object as a key and number of appearance as a value.
Thanks in advance!