Hi there,
I'm currently working on a small broject which is about providing flexible download service. Basically, I read the info from a csv file and put it in the futurelist, then depending on the length of the file I will put it either in currentlist or delayedlist and after that I want to clear the current elements from the future list, so that when I finish iterating through the futurelist it will be empty.
when I use this code It will not work and jams, I will appreciate it if somebody helped me in this


cout << "Contents: ";
list<FileInfo*>::iterator p = futurelist.begin();
while(p != futurelist.end()) {
cout << (*p)->submissiontime << " ";
cout << (*p)->length << " ";
cout << (*p)->finishtime << " ";

if(((*p)->length)<averageBW)
{
currentlist.push_back(*p);
Noactive++;
}
if(((*p)->length)>=averageBW)
{
delayedlist.push_back(*p);
Nodelayed++;
}

futurelist.remove(*p);
p++;
}
cout << "\n\n";

You do not want remove, but list::erase. Here is your code with tags and a little more cleaned up:

cout << "Contents: ";
list<FileInfo*>::iterator p = futurelist.begin();
while(p != futurelist.end()) {
  cout << (*p)->submissiontime << " ";
  cout << (*p)->length << " ";
  cout << (*p)->finishtime << " ";
  const int length = (*p)->length;
  if(length < averageBW){
   ++Noactive;
  }else{
   ++Nodelayed;
  }
   p = future.erase(p) //no need to increment p, since .erase returns the next element
}
cout << "\n\n";

But if that doesn't solve your problem, please be more specific.

EDIT: the reason why it james is because after you call .remove(*p), the iterator *p is invalid.

Edited 5 Years Ago by firstPerson: n/a

This question has already been answered. Start a new discussion instead.