queue - Ordering in Priority_Queue -
i trying learn how use priority_queue vs queue. have code priority_queue that's supposed input users. input chore , priority number of chore. can enter many want , it's supposed output them in order. problem it's not outputting them in order. i'm not sure if it's in main or in overloaded < operator function. here chore.h code:
#include<iostream> #include<fstream> using namespace std; //#ifndef chore_h; //#define chore_h; class chore{ public: chore(){prioritynum=0;chorename="";} std::string getname (chore c1)const{return chorename;} int getprioritynum(chore c1)const{return prioritynum;} bool operator <(const chore c1)const; std::ostream& output(std::ostream& cout,const chore c)const; std::istream& input(std::istream& cin); private: std::string chorename; int prioritynum; }; //#endif here's chore.cc code:
#include"chore.h" using namespace std; bool chore::operator <(const chore c1)const{ chore c2; int c1num=c1.getprioritynum(c1); int c2num=c2.getprioritynum(c2); return c1num<c2num; /*if(c1num<c2num) return true; else if(c1num==c2num) return true; else return false;*/ } std::ostream& chore::output(std::ostream& cout,const chore c)const{ cout<<endl<<"chore: "<<getname(c)<<endl; cout<<"chore priority: "<<getprioritynum(c); } std::istream& chore::input(std::istream& cin){ cout<<endl<< "enter chore name:"; cin >>chorename; cout<<endl<<"enter priority number:"; cin >>prioritynum; } my main below:
#include<queue> #include<iostream> #include"chore.h" using namespace std; int main(){ std::priority_queue<chore>mychores; chore tmp; bool enteranother=true; //input loop while(enteranother){ char c;//checks if want continue tmp.input(cin); mychores.push(tmp); cout<<endl<<"want enter chore?(y yes, n no)"; cin >>c; if(c=='y'|| c=='y') enteranother=true; else enteranother=false; } //output loop while(!mychores.empty()){ tmp = mychores.top(); mychores.top().output(cout,tmp); mychores.pop(); } } any appreciated.
Comments
Post a Comment