优先级队列C ++

时间:2018-03-18 07:13:12

标签: c++ queue priority-queue

我很难理解priority_queues,并希望有人能帮助我更好地理解它们。

我有一个名为readyQueue的priority_queue,但我并不真正了解如何将放置在其中的对象按特定顺序放置在特定的顺序中。

我有一个名为Process的结构,我用指针填充队列。

#include <string>

#define ARRAY_SIZE 10                //size for History array

using std::string;
using std::pair; using std::make_pair;

struct Process {
    string pName;                   //name of process

    pair<char, int> historyAR[ARRAY_SIZE];    //array of pairs
    pair<char, int> historySub;     //subscript for history array

    unsigned pPriority;             //nonnegative, higher values are more important

    int processID,                  //ID for process
        pArrivalTime,               //indicates when the request for process first arrived
        cpuTimer,                   //counts clock ticks til process reaches end of CPU burst 
        ioTimer,                    //counts clock ticks til process reaches end of I/O burst 
        cpuTotal,                   //accumulates number of ticks the process spends active
        iTotal,                     //accumulates number of ticks the process spends Iactive
        oTotal,                     //accumulates number of ticks the process spends Oactive
        cpuCount,                   //counts the CPU burst for this process
        iCount,                     //counts the input burst for this process
        oCount;                     //counts the output burst for this process


    Process(string _pName, unsigned _pPriority, int _processID, int _pArrivalTime, int inAR[], char charAR[]);

    void increaseCPUTimer();
    void increaseIOTimer();
    void printBurst();

    bool operator<(const Process& rhs) const;   
};

bool Process::operator<(const Process& rhs) const {
    return this->pPriority > rhs.pPriority;
}

在我的主要文件中有两个队列声明,

queue<Process*> entryQueue;                           
priority_queue<Process*> readyQueue;

我正在添加我的priority_queue,

while(readyQueue.size() < AT_ONCE && entryQueue.size() > 0) {
    readyQueue.push(entryQueue.front());
    entryQueue.pop();
}

但是我不知道如何“排序”它们,在插入时,使用较高pPriority值(无符号数)的过程被放置在priority_queue的前面。

0 个答案:

没有答案