【问题标题】:Pushing items from a text file into queue of struct types将文本文件中的项目推送到结构类型队列中
【发布时间】:2016-11-09 18:02:52
【问题描述】:
#include<iostream>
#include<fstream>
#include<queue>

using namespace std;

const int process_cnt=22;


struct process{

  int at;
  float bt;
  float rt;
  int tat;
  int wt;};

queue<process*> ready_q;
queue<process*> init_q;

int main(){

  ifstream inData;

   inData.open("input.txt");
   while(inData){

我认为这是导致错误的原因,因为不应将文件中的条目放入当前的前面。顺便说一句,该文件在每行中有两个由空格分隔的值。条目指针指向前面的元素。我认为指向 back() 的入口可能有效,但效果不佳。

   process *entry = init_q.front();//ERROR


   inData >>entry->at>>entry->bt;
   init_q.push(entry);
   }
  cout<<"Read Successful in read_file"<<endl;

  inData.close();
 return 0;
}

【问题讨论】:

  • 在互联网上搜索“StackOverflow c++ 读取文件结构”以获取一些示例。
  • 你为什么将 pointers 推送到对象而不是对象实例?指向对象的指针可能会导致内存泄漏的噩梦。此外,如果您坚持使用指针,请使用智能指针。

标签: c++ struct queue


【解决方案1】:

仅解决标记为“//ERROR”的问题。 此时 init_q 为空且 front() 未定义。 查看 deque 的文档,队列的默认容器。 cppreference

【讨论】:

    猜你喜欢
    • 2022-06-10
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 2016-05-31
    相关资源
    最近更新 更多