map<struct,int>这种情况以前没有应对会,涉及到find方法就会报错。
class Foo { int a1; int a2; //方案一 bool operator<(const Foo& other) const //类字典序比较方式 { return ((a1<other.a1) || (!(a1>other.a1) && (a2<other.a2));
}
}
当有两个元素时,需要重载<运算符。
a1小于成立时,或者a1不小于不成立但是a2小于成立。
左边成立,右边必然不成立。左边不成立,右边第一个肯定成立,再判断第二个。
map基于红黑树,插入时遍进行排序,所以对于结构体需要重载小于运算符。
策略是:先以a1排序,a1排完后根据a2排序。