从文件中读取优先级队列 C++
priority queue read from file c++
我有一个优先级队列和一个 "event" class,我想从一个名为 agenda.txt 的文件中读取,其中我有:优先级、日期、年份和事件名称.但是当我从文件中读取时,我只得到第一个元素,我想看到优先级最高的元素。你能帮忙吗?
agenda.txt
9
12.05
2016
meeting
16
13.05
2017
shopping
8
12.09
2056
swimming
60
45.76
2016
work
这是主要的:
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
不知道如何排序您的自定义 class。您 Class 需要覆盖小于运算符,以便 priority_queue
可以按照文本文件中定义的优先级对项目进行排序。一个伪实现看起来像这样:
class Event
{
int Priority;
};
bool operator<(const Event& lhs, const Event& rhs) {return (lhs.Priority < rhs.Priority);}
如果我理解正确的话,你想要的是让文件中的第一条记录成为具有最高优先级的记录;如果是这种情况,那么您应该按照优先级顺序向文件写入记录,第一条记录具有最高优先级。例如
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';
}
但是,如果您的文件中的记录以某种随机顺序排列,您将无能为力(如果使用优先级队列),只能读取所有记录并跟踪具有最高优先级的记录。所以你会做以下事情
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
}
}
如果您希望文件中的所有记录按排序顺序排列,您必须将它们全部读入 vector
并调用 vector
[= 上的 std::sort
方法17=]
如果您知道元素在文件中的位置,您可以在 ifstream
对象中打开文件,然后对该对象调用 seekg
方法以从该点开始读取.
我有一个优先级队列和一个 "event" class,我想从一个名为 agenda.txt 的文件中读取,其中我有:优先级、日期、年份和事件名称.但是当我从文件中读取时,我只得到第一个元素,我想看到优先级最高的元素。你能帮忙吗?
agenda.txt
9
12.05
2016
meeting
16 13.05
2017
shopping
8 12.09
2056
swimming
60 45.76
2016
work
这是主要的:
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
不知道如何排序您的自定义 class。您 Class 需要覆盖小于运算符,以便 priority_queue
可以按照文本文件中定义的优先级对项目进行排序。一个伪实现看起来像这样:
class Event
{
int Priority;
};
bool operator<(const Event& lhs, const Event& rhs) {return (lhs.Priority < rhs.Priority);}
如果我理解正确的话,你想要的是让文件中的第一条记录成为具有最高优先级的记录;如果是这种情况,那么您应该按照优先级顺序向文件写入记录,第一条记录具有最高优先级。例如
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';
}
但是,如果您的文件中的记录以某种随机顺序排列,您将无能为力(如果使用优先级队列),只能读取所有记录并跟踪具有最高优先级的记录。所以你会做以下事情
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
}
}
如果您希望文件中的所有记录按排序顺序排列,您必须将它们全部读入 vector
并调用 vector
[= 上的 std::sort
方法17=]
如果您知道元素在文件中的位置,您可以在 ifstream
对象中打开文件,然后对该对象调用 seekg
方法以从该点开始读取.