C++ 如何在优先级队列中使用自定义类?
Main.cppC++ 如何在优先级队列中使用自定义类?,c++,iterator,priority-queue,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<funct
#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;
}
错误:
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){
^
hello-cpp-world.cc:在函数“void FCFS(FCFS_q,int,int,int)”中:
您好cpp world.cc:39:9:错误:“迭代器”不是“fcfs_q{aka std::priority_queue}”的成员
对于(fcfs_q::迭代器it=procs.begin();it!=procs.end(),++it{
^
您好,cpp world.cc:39:26:错误:预期为“;”在“it”之前
对于(fcfs_q::迭代器it=procs.begin();it!=procs.end(),++it{
^
您好cpp world.cc:39:46:错误:“它”未在此范围内声明
对于(fcfs_q::迭代器it=procs.begin();it!=procs.end(),++it{
^
您好cpp world.cc:39:57:错误:“fcfs_q”没有名为“end”的成员
对于(fcfs_q::迭代器it=procs.begin();it!=procs.end(),++it{
^
您好,cpp world.cc:39:68:错误:在“{”标记之前应为“;”
对于(fcfs_q::迭代器it=procs.begin();it!=procs.end(),++it{
^
您好cpp world.cc:39:68:错误:在“{”标记之前应该有主表达式
您好,cpp world.cc:39:68:错误:在“{”标记之前应为“')
您好cpp world.cc:40:25:错误:“fcfs_q”没有名为“end”的成员
if(it==(procs.end()-1){
^
你好,cpp world.cc:40:35:错误:在“{”标记之前应为“')
if(it==(procs.end()-1){
^
您好cpp world.cc:46:5:错误:在“}”标记之前应该有主表达式
}
^
您好,cpp world.cc:46:5:错误:应为“;”before“}”标记
hello-cpp-world.cc:在全球范围内:
您好cpp world.cc:49:10:错误:变量或字段“SJF”声明无效
无效SJF(sfj_q过程,整数n,整数m,整数t_cs){
^
您好cpp world.cc:49:10:错误:“sfj_q”未在此范围内声明
您好cpp world.cc:49:23:错误:在“int”之前应该有主表达式
无效SJF(sfj_q过程,整数n,整数m,整数t_cs){
^
您好cpp world.cc:49:30:错误:应在“int”之前使用主表达式
无效SJF(sfj_q过程,整数n,整数m,整数t_cs){
^
您好cpp world.cc:49:37:错误:在“int”之前应该有主表达式
无效SJF(sfj_q过程,整数n,整数m,整数t_cs){
^
有了上面的代码块和一个定义的procedure.h文件,我得到了一个错误,说我的typedef迭代器不是一个有效的类。我不知道是什么导致了这一错误。有人能在我上面的代码中看到错误吗?
std:queue
和std::priority\u queue
都是不可测试的,因此您尝试的类型和方法是不可测试的o调用未定义。您可以在队列上模拟迭代,如下图所示,但请注意:此操作将销毁队列。由于您是按值传递队列对象,而不是按引用传递队列对象,因此不会销毁原始对象,只销毁副本,但复制本身可能需要一些时间
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();
}
}
void FCFS(FCFS_q procs,int n,int m,int t_cs){
cout在发布有关生成错误的问题时,请在问题正文中包含实际的、完整的、完整的和未编辑的错误输出,包括任何信息注释。这与优先级队列相同,例如,int
。与您自己的类型无关。有关可用操作,请咨询a。我已在@JoachimPilebo更新了它rgPriority队列没有迭代器请参见此处:和此处:简言之:std:queue
和std::priority\u 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();
}
}