跳到正文

个人记录

重载运算符

DavidLI约 1 分钟
原文
文章信息与版本

文章信息

发布
2017年12月16日
收录
2026年7月5日
更新
2026年10月17日
篇幅
约 1 分钟

版本

  1. v1当前

###格式

<返回类型>operator<运算符号>(参数){

<函数内容>

代码
}

#####(常用在优先队列里)

Example1

C++
     struct purple{
        int id;
        int age;
        bool operator<(const purple &a)const{
            if(a.age==age) return a.id<id;
            return a.age<age;
        }
    };
    //年龄(age)从小到大排,当年龄相同时编号(id)从小到大排

Example2

C++
    struct Edge{
        int id,d;
        bool operator<(const Edge &k)const{
            return k.d<d;
        }
    };
[return k.d<d:最后排序结果中为降序序列(d1<d2<d3<......<di)]
原文