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";

Recommended Answers

All 2 Replies

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.

It worked totally fine, thank you very much sir

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.