对于列表排序,有没有办法使用相同的参数让多个运算符重载?

For list sorting, is there a way to have multiple operator overloads with the same arguments?

本文关键字:参数 重载 运算符 列表 于列表 排序 有没有      更新时间:2023-10-16

摘要

我有一个 std::列表类型为进程*

class Process
{
// non essential stuff
// vars I want to sort by
int pid;
int burstTime;
int rBurstTime;
int priority;
}

我想重载

bool operator<(Process const& p) {return this.priority < p.priority}
bool operator<(Process const& p) {return this.burstTime < p.burstTime}
// etc.

以上似乎是不可能的,因为没有办法确定两者之间的差异(或者我是否在正确的轨道上?

我尝试了什么

我试过类似的东西

bool operator<(Process const& p, <k>) {return this.priority < p.priority}

其中 k 只是任何数据类型/期望值,它告诉使用哪个重载,但这是不可能的,因为重载只<一个参数。>

希望到现在为止,您能看到我想做什么。是否有我不知道的C++程序?我是一个相对较新的C++程序员,所以如果这是一个简单的解决方案,请道歉。

通过 Borgleader 的评论解决:

std::list 的排序可以采用比较函数/函子,你应该使用它(std::sort 也是如此)