一个Person类 没有重载 == 只写了 < 的仿函数 ,然后 用set 存 Person对象
我没重写== , set 的find 是怎么判断 两个Person相等的?而且不报错
class Person{
public:
int age;
string name;
Person(){}
Person(int a,string s):age(a),name(s){
}
~Person(){
}
/*const bool operator==(const Person& p) const{
return (this->age == p.age);
}*/
};
class mycomp{
public:
bool operator()(const Person& a,const Person& b){
return a.age < b.age;
}
};
a == b
可以等价于!(a < b) && !(b < a)
https://en.cppreference.com/w/cpp/container/set
Everywhere the standard library uses the Compare requirements, uniqueness is determined by using the equivalence relation. In imprecise terms, two objects a and b are considered equivalent if neither compares less than the other: !comp(a, b) && !comp(b, a).
看stl源代码
应该有两个实现,一种就是比较内存地址一样,一种就是比较所有成员值都相同