简体   繁体   English

优先级队列从文件c ++中读取

[英]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. 我有一个优先级队列和一个“事件”类,我想读取一个名为agenda.txt的文件,其中包括:优先级,日期,年份和事件名称。 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 agenda.txt
9 9
12.05 12.05
2016 2016
meeting 会议
16 13.05 16 13.05
2017 2017年
shopping 购物
8 12.09 8 12.09
2056 2056
swimming 游泳的
60 45.76 60 45.76
2016 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");}}

priority_queue does not know how to sort your custom class. priority_queue不知道如何对自定义类进行排序。 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. 您的类需要覆盖小于运算符,以便priority_queue可以按文本文件中定义的优先级对项目进行排序。 A pseudo-implementation would look like this: 伪实现看起来像这样:

class Event
{
   int Priority;
};
bool operator<(const Event& lhs, const Event& rhs) {return (lhs.Priority < rhs.Priority);}

If I am understanding correctly what you want is to have the first record in the file be the record with the highest priority; 如果我正确理解你想要的是将文件中的第一条记录作为具有最高优先级的记录; if that is the case then you should be writing records in order of priority to the file with the first record having the highest priority. 如果是这种情况,那么您应该按优先顺序写入记录,第一条记录具有最高优先级。 So for example 所以举个例子

class Event {
public:
    int priority;
    int date;
    int year;
    string name;
};

bool operator<(const Event& first, const Event& second) { 
    return first.priority < second.priority;
}

priority_queue<event> pq;

for (const auto& event : events_array_from_before) {
    pq.push(event);
}

ofstream fout;
fout.open("agenda.txt");
if (!fout) {
    cerr << "Could not open file to write to" << endl;
    return 1;
}

while (!pq.empty()) {
    const auto& event = pq.top();
    pq.pop();
    fout << event.priority << '\n' << event.date << '\n' 
         << event.year << '\n' << event.name << '\n';
}

But if you have the file with the records in some random order you can do nothing (if using a priority queue) but to read all of the records and keep track of the one with the highest priority. 但是如果你的文件包含一些随机顺序的记录,你就什么都不做(如果使用优先级队列),而是读取所有记录并跟踪具有最高优先级的记录。 So you would do the following 所以你会做以下事情

ifstream fin;
fin.open("agenda.txt");
if (!fin) { 
    cerr << "Could not open file agenda.txt" << endl;
    return 1;
}

Event temp;
Event top_record;
fin >> top_record.priority >> top_record.date >> top_record.year 
    >> top_record.name;
while (fin >> temp.priority >> temp.date >> temp.year >> temp.name) {
    if (temp.priority > top_record.priority) {
        top_record = std::move(temp); // for efficient moving of string name
    }
}

If you want all the records from the file in sorted order you will have to read all of them into a vector and call the std::sort method on the vector 如果你想从排序的顺序文件中的所有记录,你将不得不他们都读入一个vector和调用std::sort方法上的vector

If you have some indication of where the element is in the file you could open the file in a ifstream object and then call the seekg method on the object to read from that point on. 如果您有一些关于元素在文件中的位置的指示,您可以在ifstream对象中打开该文件,然后在该对象上调用seekg方法以从该点开始读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM