Register__ 2022-02-22 15:49 采纳率: 89.5%
浏览 20
已结题

C++ set容器问题

一个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;
    }
};
  • 写回答

3条回答 默认 最新

  • _GX_ 2022-02-22 16:34
    关注

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月2日
  • 已采纳回答 2月22日
  • 创建了问题 2月22日

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题