Re: Is this a valid less than operator for a map?
On Aug 22, 8:46 pm, "dimsttho...@yahoo.com" <dimsttho...@yahoo.com>
wrote:
I can declare and use a map variable using this operator
std::map<MyClass,std::string> m_MyClassCache;
...
Yes, but the problem is the class I am using has a lot of member data
and already has an == operator and I wanted to avoid having to write =
a
fully fledged < operator. See my other reply.
I might be wrong here, but I can't help forming the impression that
you might be misusing map. Please excuse me if I'm off base. The
thing is - you normally want to put a smallish key on the left in your
map instantiation, representing only those parts of the data that are
effectively the unique, unchanging identity (key) for your data. On
the right, you put all the values that change while the program runs,
where such changes don't change the fundamental identity of the entity
involved. For example, your name might be a key, but you can change
address, phone number, age etc. without ceasing to be you, and these
latter qualities should be grouped in a class/struct/whatever and on
the right in the instantiation.
That you say you have a lot of data members to compare in the
operator== makes it clear you have a very large key. But you're happy
to have a very arbitrary ordering of the keys. It may still be that
you can define operator< on a single inherently unique data member,
providing you won't change that data member without deleting/
reinserting the element in the map (you can't change the ordering
without corrupting the map). It may be that a cascading comparison of
a few fields like Ron suggests is sufficient. You generally don't
need to compare every field the way you do for operator==. For
example, with the personal information mentioned above, you can
typically order by name with no reference to the other fields. For
larger apps, you might want to differiate same-named people with
another field (birthday, passport number, NSA id, ...).
If you do need to compare everything, then one common factoring is to
put in an int compare(const My_Class& rhs) const member that returns a
negative number, 0 or a positive number indicating <, ==, or >
respectively. Then you can implement all the normal comparison
operators as trivial calls to compare.
There's also a larger question of ownership or duplication of data.
Your map<My_Class, string> involves keeping a complete set of all the
data in order to be able to map from a My_Class value to a string. If
the My_Class data is already been kept elsewhere anyway, this is
overkill and hurts performance, as well as being an extra data
repository to sync when changes must be made. Typically, you want to
do something like this:
struct My_Class_Key
{
My_Class_Key(const My_Class& x)
: key1_(x.get_key1()), key2_(x.get_key2())
{ }
bool operator<(const My_Class_Key& rhs) const
{
return key1_ < rhs.key1_ or
key1_ == rhs.key1_ and key2_ < rhs.key2_;
}
My_Class::Key1 key1_;
My_Class::Key2 key2_;
};
Then, when you have a My_Class object x and want to find the
associated string, use:
My_Map::iterator i = my_map.find(My_Class_Key(x));
Alternatively, you can provide a function in My_Class that returns a
unique key object: sometimes this centralisation is good, but then
sometimes the key fields or ordering differs for different users.
If you want more specific guidance, perhaps post a list of your data
members, and explain where else (if anywhere) they're stored in the
application, so we can get a feel for things.
HTH,
Tony