【发布时间】:2014-06-20 17:44:19
【问题描述】:
这是我第一次在这个网站上提问,所以如果我违反了任何礼仪,请告诉我。
我对链表真的很陌生,并认为使用 list 数据类型实现它们会非常简单,但似乎我正试图对它们做一些奇怪的事情。
我创建了一个名为“eventList”的list,我想将一系列名为“entry”的structs 放入其中,然后我希望将几个“eventList”放入一个名为“processList”的数组中.
我的问题是那段代码
processList[pid].push_front(newEntry);
似乎给了我一个错误。我在做什么明显有问题吗?
代码如下:
#include <iostream>
#include <fstream>
#include <list>
#include <string>
#include <array>
using namespace std;
struct entry {
int eTime;
string eType;
int pid;
};
int main(){
ifstream infile;
string comm;
int num;
entry newEntry;
list<entry> eventList;
array<list<entry>, 1024> processList;
infile.open("input00.txt");
if(infile.is_open()){
while(!infile.eof()){
int pid = -1;
string eTy;
int eTi;
infile >> eTy;
infile >> eTi;
if(eTy == "NEW"){
pid++;
cout << "DB: Process " << pid << endl;
}
else{
newEntry.pid = pid;
newEntry.eType = eTy;
cout << "Type: " << newEntry.eType << " | ";
newEntry.eTime = eTi;
cout << "Time: " << newEntry.eTime << endl;
processList[pid].push_front(newEntry);
//processList[pid] = eventList; <- realized that that wouldn't work fairly quickly
}
}
}
else {
cout << "Sorry, that file doesn't work. :[" <<endl;
}
cout << "Original Order:" << endl;
list<entry>::iterator p = eventList.begin();
while(p != eventList.end()){
cout << p->eType << " For " << p->eTime << "\n"; //This comes from http://www.cplusplus.com/forum/beginner/3396/
p++;
}
//cout << "Ordered By Time:\n";
//eventList.sort(); <- commented out, because I can't get it to work. :[
system ("PAUSE");
return 0;
}
我真的很喜欢这个资源是可用的,我希望我可以遵循任何以这种方式出现的答案的逻辑。抱歉,感谢您的观看!
【问题讨论】:
-
您好,欢迎来到本站。你得到的错误是什么?我相信您在问题中遗漏了一些信息 - 实际上,我并没有真正了解问题本身。
-
对不起,我刚刚编辑了这个问题,更具体地说,我的程序正在退出“返回值 3221225477”,这对我来说意义不大,但我想我确实在某处看到了 segfault 这个词。我的假设是我试图将一些东西放入一个不希望任何东西进入的内存地址中。
-
是时候启动调试器并单步执行您的代码了。
-
我的猜测是,您第一次尝试使用 pid = -1 将条目添加到列表中,因此尝试引用数组 [-1]。但这是一个猜测。无论您使用哪种调试器,都会肯定地告诉您。
-
你也可以fix this:
while(!infile.eof())在那里。