我目前有一个队列,其中包含用户指定数量的结构,称为Process。进程由 pid、突发和到达组成。我想按到达时间对队列进行排序,但我完全不知道从哪里开始。这是一些伪代码来帮助说明我想说的内容:
struct Process{
int pid;
int burst;
int arrival;
};
void function(int numProcesses){
queue<Process> readyQueue;
// The following loop is a shortened version of my code
for(int i=0; i<numProcesses;i++){
readyQueue.push(aProcess);
}
// This is where I need help!
// sort(readyQueue);
}
我将不胜感激任何能够为我指明如何做到这一点的正确方向的人,或者是否有可能。谢谢!
大多数情况下你需要定义operator<
对于你的班级:
struct Process{
int pid;
int burst;
int arrival;
bool operator<(Process const &other) { return arrival < other.arrival; }
};
一旦你做到了这一点,std::sort
会工作得很好:
std::sort(std::begin(readyQueue), std::end(readyQueue));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)