/*
QMap uses qMapLessThanKey() to compare keys. The default
implementation uses operator<(). For pointer types,
qMapLessThanKey() casts the pointers to integers before it
compares them, because operator<() is undefined on pointers
that come from different memory blocks. (In practice, this
is only a problem when running a program such as
BoundsChecker.)
*/
template <class Key> inline bool qMapLessThanKey(const Key &key1, const Key &key2)
{
return key1 < key2;
}
QMap在比较key时使用了
qMapLessThanKey()函数模板,从函数体可以看出调用了类的小于符,因此在使用类对象作为key时必须重载小于运算符。例如:
struct s_test{int a;int b;bool operator< (const s_test& other) const //注: const 不能省{
return (a < other.a && b < other.b);}
};