Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

priority queue read from file c++

I have a priority queue and an "event" class and i want to read from a file called agenda.txt where i have: priority,date,year and name of the event. But when i read from the file i get only the first element and i want to see the element with the highest priority. Can you help?

agenda.txt
9
12.05
2016
meeting
16 13.05
2017
shopping
8 12.09
2056
swimming
60 45.76
2016
work

and this is the main:

int main(){

char filename[50];
ifstream bucky;
cin.getline(filename,50);
bucky.open(filename);
if(!bucky.is_open()){
    exit(EXIT_FAILURE);
}
string nume;
int prio;
double data;
int an;
bucky>>prio>>data>>an>>nume;

while(bucky.good()){
cout<<"prioritatea este "<<prio<<"    data este "<<data<<"   anul este "<<an<<"   numele este  "<<nume<<" "<<endl;
bucky>>prio>>data>>an>>nume;



priority_queue<Event> q;

q.push(Event(prio,data,an,nume));


    cout<< q.top().getEventPriority()<<q.top().getEventData()<<" "<<q.top().getEventAn()<<" "<<q.top().getEventName()<<endl;



system("pause");}}
like image 567
Raluca lok Avatar asked Sep 25 '26 07:09

Raluca lok


1 Answers

priority_queue does not know how to sort your custom class. You Class needs to override the less than operator so that the priority_queue can sort the items by the Priority defined in your text file. A pseudo-implementation would look like this:

class Event
{
   int Priority;
};
bool operator<(const Event& lhs, const Event& rhs) {return (lhs.Priority < rhs.Priority);}
like image 139
Robben_Ford_Fan_boy Avatar answered Sep 28 '26 06:09

Robben_Ford_Fan_boy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!