Afficher un message
Vieux 21/02/2008, 17h58   #6
Eric Pruneau
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: vector.erase(iterator iter) will change "iter" or not?


"thomas" <FreshThomas@gmail.com> a écrit dans le message de news:
c69c72bd-a326-4bc5-b1ef-4dd03288d4f4...oglegroups.com...
>
>> (And the higher-level answer to his question is that he should probably
>> be using std::remove anyway:
>>
>> s.erase(remove(s.begin(), s.end(), 3), s.end());
>>

>
> What's this?
> will "s.erase()" get all the elements after "3" removed?


remove will remove all element equal to 3. beware, removing is not
erasing... Remove returns an iterator to the new end of the sequence s (it
does not actually modify s.end() ) , so you can use this iterator in the
vector::erase function. The size of the vector is not modified after a
remove, but the element between the returned end and the actual s.end() but
the element are unspecified.

so you can do

vector<int>::iterator NewEnd = remove(s.begin(), s.end(), 3);
s.erase(NewEnd, s.end);


Eric Pruneau


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