gpt4 book ai didi

c++ - 将结构链表添加到数组会产生错误

转载 作者:行者123 更新时间:2023-11-28 06:49:11 24 4
gpt4 key购买 nike

这是我第一次在这个网站上提问,所以如果我违反了任何礼节,请告诉我。

我对链表真的很陌生,我认为使用 list 数据类型实现它们会非常简单,但我似乎想用它们做一些奇怪的事情。

我创建了一个名为“eventList”的list,我想在其中放入一系列名为“entry”的struct,然后我希望放入几个“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;
}

我真的很喜欢这个资源可用,我希望我能遵循以这种方式出现的任何答案的逻辑。抱歉给您带来的笨拙,感谢您的观看!

最佳答案

您正在 while 循环内初始化 pid。所以无论你做什么,你都会引用数组[-1]。与你的调试器交谈,它会证明我是对的。

关于c++ - 将结构链表添加到数组会产生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24332872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com