By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,985 Members | 1,830 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,985 IT Pros & Developers. It's quick & easy.

List iterator not incrementable!!!!

P: 2
Hi guys
correct me plz:
i have this error just when i put liste.erase(it);

Debug assertion Failed
....
Expression: List iterator not incrementable.

Expand|Select|Wrap|Line Numbers
  1. for(list<student>::iterator it=liste.begin();it!=liste.end();it++){
  2.     if((it->getsexe()=='f')||(it->getsexe()=='F')){
  3.             tab[j].setNom(it->getNom());
  4.  
  5.             liste.erase(it);
  6.             j++;
  7.         }
  8. }
  9. for(list<student>::iterator it=liste.begin();it!=liste.end();it++){
  10.         it->display();
  11.         cout<<endl;
  12.     }
Can anyone plz help me...


Thank u
Aug 28 '10 #1

✓ answered by newb16

Because erase invelidates iterator to erased element

all of the previously obtained iterators and references remain valid after the erasing operation and refer to the same elements they were referring before (except, naturally, for those referring to erased elements).

The code may use the return value of erase:
Expand|Select|Wrap|Line Numbers
  1. for(...;...;/*no it++ here*/)
  2. {
  3.   if (condition)
  4.   {
  5.     it = list.erase(it);
  6.   }
  7.   else
  8.     it++;
  9. }

Share this Question
Share on Google+
2 Replies


100+
P: 687
Because erase invelidates iterator to erased element

all of the previously obtained iterators and references remain valid after the erasing operation and refer to the same elements they were referring before (except, naturally, for those referring to erased elements).

The code may use the return value of erase:
Expand|Select|Wrap|Line Numbers
  1. for(...;...;/*no it++ here*/)
  2. {
  3.   if (condition)
  4.   {
  5.     it = list.erase(it);
  6.   }
  7.   else
  8.     it++;
  9. }
Aug 28 '10 #2

P: 2
thank u so much :)
Aug 28 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.