Afficher un message
Vieux 21/02/2008, 18h45   #8
Martin York
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: vector.erase(iterator iter) will change "iter" or not?


Just a small correction.

> vector<int> s;
> for(vector<int>::iterator iter=s.begin(); iter!=s.end(); iter++){
> if(*iter==3)
> iter= s.erase(iter);
> }


After the erase() call iter points at the next element in the
container. Thus when the iter++ is called you will effectively skip
an element or if the erased element is the last element it now points
one after end().

Also for efficiency it is a god idea to get in the habit of using
prefix increment (In this case it probably makes no difference but
for the generic case it does).

  Réponse avec citation
 
Page generated in 0,04495 seconds with 9 queries