如何在优先级队列中使用自定义类?

时间:2016-02-15 08:05:47

标签: c++ iterator priority-queue

Main.cpp的

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <queue>
#include<functional>
#include "procedure.h"

using namespace std;


int gcounter = 0;

//Comparator Classes
struct ComparebyJobTime{
        bool operator()(Procedure lhs,Procedure rhs) {
            return lhs.getTime() < rhs.getTime();
        }
};

struct ComparebyProc{
        bool operator()(Procedure lhs, Procedure rhs){
            return lhs.getProc() < rhs.getProc();
        }
};


typedef priority_queue<Procedure, vector<Procedure>, ComparebyProc> fcfs_q;
typedef priority_queue<Procedure, vector<Procedure>, ComparebyJobTime> sjf_q;

//Scheduling Algorithms
void FCFS(fcfs_q procs, int n, int m, int t_cs){
    cout << "time " << gcounter << "ms: Simulator started for FCFS [Q ";
    for(fcfs_q::iterator it = procs.begin(); it!= procs.end(), ++it{
        if(it == (procs.end() - 1){
            cout << *it.getProc() << "]" << endl;
        }
        else{
            cout << *it.getProc() << " ";
        }
    }
}

void SJF(sfj_q procs, int n, int m, int t_cs){
    cout << "time " << gcounter << "ms: Simulator started for SJF [Q ";
    for(sjf_q::iterator it = procs.begin(); it != procs.end(); ++i{
        if(it == (procs.end() - 1){
            cout << *it.getProc() << "]" << endl;
        }
        else{
            cout << *it.getProc() << " ";
        }
    }
}

//Main function
int main(int argc, char * argv[]) {
    if (argc != 3){
        cerr << "Usage: " << argv[0] << "processes-file\n";
    }
    ifstream in_file(argv[1]);

    if(!in_file.good()){
        cerr << "Can not open the input file " << argv[1] << "\n";
    }
    ofstream out_str(argv[2]);
    if(!out_str){
        cerr << "Could not open " << argv[2] << "to write\n";
    }

    string line;
    fcfs_q pqf;
    sjf_q pqs;

    while(getline(in_file, line)){
        istringstream iss(line);
        char com;
        iss >> com;
        if(line.length() == 0 || com == '#'){
            continue;
        }
        vector<string> split;
        string token;
        stringstream temp(line);
        while(getline(temp, token, '|')){
            split.push_back(token);
        }
        pqf.push(Procedure(atoi(split[0].c_str()),atoi(split[1].c_str()),atoi(split[2].c_str()),atoi(split[3].c_str())));
        pqs.push(Procedure(atoi(split[0].c_str()),atoi(split[1].c_str()),atoi(split[2].c_str()),atoi(split[3].c_str())));
    }
    //Simulation Start
    int n = pqf.size();
    int m = 1; //Number of processors
    int t_cs = 9; //Default context switch time
    FCFS(pqf, n, m, t_cs);
    cout << "time " << gcounter << "ms: Simulator ended for FCFS" << endl;
    SJF(pqs, n, m, t_cs);
    cout << "time " << gcounter << "ms: Simulator ended for SJF" << endl;
}

procedures.h

class Procedure{
    public:
        Procedure();
        Procedure(int p, int bt, int num, int io){
            procNum = p;
            burst = bt;
            numBurst = num;
            ioTime = io; 
        }
        int getProc(){return procNum;}
        int getNum(){return numBurst;}
        int getIO(){return ioTime;}
        int getTime(){return burst;}

    private:
        int procNum;
        int numBurst;
        int ioTime;
        int burst;
};

错误:

hello-cpp-world.cc: In function ‘void FCFS(fcfs_q, int, int, int)’:
hello-cpp-world.cc:39:9: error: ‘iterator’ is not a member of ‘fcfs_q {aka std::priority_queue<Procedure, std::vector<Procedure>, ComparebyProc>}’
     for(fcfs_q::iterator it = procs.begin(); it!= procs.end(), ++it{
         ^
hello-cpp-world.cc:39:26: error: expected ‘;’ before ‘it’
     for(fcfs_q::iterator it = procs.begin(); it!= procs.end(), ++it{
                          ^
hello-cpp-world.cc:39:46: error: ‘it’ was not declared in this scope
     for(fcfs_q::iterator it = procs.begin(); it!= procs.end(), ++it{
                                              ^
hello-cpp-world.cc:39:57: error: ‘fcfs_q’ has no member named ‘end’
     for(fcfs_q::iterator it = procs.begin(); it!= procs.end(), ++it{
                                                         ^
hello-cpp-world.cc:39:68: error: expected ‘;’ before ‘{’ token
     for(fcfs_q::iterator it = procs.begin(); it!= procs.end(), ++it{
                                                                    ^
hello-cpp-world.cc:39:68: error: expected primary-expression before ‘{’ token
hello-cpp-world.cc:39:68: error: expected ‘)’ before ‘{’ token
hello-cpp-world.cc:40:25: error: ‘fcfs_q’ has no member named ‘end’
         if(it == (procs.end() - 1){
                         ^
hello-cpp-world.cc:40:35: error: expected ‘)’ before ‘{’ token
         if(it == (procs.end() - 1){
                                   ^
hello-cpp-world.cc:46:5: error: expected primary-expression before ‘}’ token
     }
     ^
hello-cpp-world.cc:46:5: error: expected ‘;’ before ‘}’ token
hello-cpp-world.cc: At global scope:
hello-cpp-world.cc:49:10: error: variable or field ‘SJF’ declared void
 void SJF(sfj_q procs, int n, int m, int t_cs){
          ^
hello-cpp-world.cc:49:10: error: ‘sfj_q’ was not declared in this scope
hello-cpp-world.cc:49:23: error: expected primary-expression before ‘int’
 void SJF(sfj_q procs, int n, int m, int t_cs){
                       ^
hello-cpp-world.cc:49:30: error: expected primary-expression before ‘int’
 void SJF(sfj_q procs, int n, int m, int t_cs){
                              ^
hello-cpp-world.cc:49:37: error: expected primary-expression before ‘int’
 void SJF(sfj_q procs, int n, int m, int t_cs){
                                     ^

使用上面的代码块和定义的procedure.h文件,我收到的错误是我的typedef迭代器不是有效的类。我无法弄清楚造成这种情况的原因是什么,任何人都可以在上面的代码中看到错误?

1 个答案:

答案 0 :(得分:0)

std:queuestd::priority_queue都不可迭代,因此您未尝试调用的类型和方法未定义。您可以模拟队列上的迭代,如下图所示,但要注意:此操作将破坏队列。由于您按值而不是通过引用传递队列对象,原始的不会被销毁,只有副本,但复制本身可能需要一些时间。

void FCFS(fcfs_q procs, int n, int m, int t_cs){
    cout << "time " << gcounter << "ms: Simulator started for FCFS [Q ";
    while (!fcfs.empty()) {
        const auto& elem = fcfs.top();
        if(fcfs.size() == 1){ // the last element in the queue
            cout << elem.getProc() << "]" << endl;
        }
        else{
            cout << elem.getProc() << " ";
        }
        fcfs.pop();
    }
}

有关详细信息,请参阅How to iterate over a priority_queue?STL queue iteration

另外,也许可以查看STL堆算法,例如make_heap()中的push_heap()pop_heap()sort_heap()<algorithm>。这些允许您明确地将项目存储为您选择的容器,保留其所有属性,例如,迭代它的能力,同时保持它的排序。

相关问题