Saturday, 15 March 2014

c++ - Adding a linked list of structs to an array produces an error -



c++ - Adding a linked list of structs to an array produces an error -

this first time asking on site, if breach sort of etiquette, allow me know.

i new linked lists , thought implementing them using list datatype pretty straightforward, seems i'm trying weird them.

i have created list called "eventlist" want set series of structs called "entry", , wish set several "eventlist"s array called "processlist".

my issue bit of code

processlist[pid].push_front(newentry);

seems give me error. there wrong i'm doing?

here code:

#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 wouldn't work } } } else { cout << "sorry, file doesn't work. :[" <<endl; } cout << "original order:" << endl; list<entry>::iterator p = eventlist.begin(); while(p != eventlist.end()){ cout << p->etype << " " << p->etime << "\n"; //this comes http://www.cplusplus.com/forum/beginner/3396/ p++; } //cout << "ordered time:\n"; //eventlist.sort(); <- commented out, because can't work. :[ scheme ("pause"); homecoming 0; }

i love resource available, , hope can follow logic of answers come way. apologies noobishness, , looking!

if ety != "new" pid remains equal -1.

so,

processlist[pid].push_front(newentry);

will crash because subscript out of bounds ( -1 )

c++ arrays list stl

No comments:

Post a Comment