Re: vector.erase(iterator iter) will change "iter" or not?
In message <fpk2sg$5du$1@news.datemas.de>, Victor Bazarov
<v.Abazarov@comAcast.net> writes
>thomas wrote:
>> suppose I will delete an element pointed to by "iter".
>> like this:
>>
>> vector<int> s;
>> for(vector<int>::iterator iter=s.begin(); iter!=s.end(); iter++){
>> if(*iter==3)
>> s.erase(iter); //A
>> }
>>
>> in line A, if element by "iter" is erased, will "iter" point to the
>> next element(now should be the current element) automatically?
>
>The iterator that refers to the removed element and all elements
>after the removed one are invalidated by that operation. IOW, the
>Standard makes no attempt to define what the 'iter' would point to
>after being erased.
I wonder if the OP is confused because the iterator is passed by value
and therefore not modified? Obviously erase(iter) can't change its
_value_, but it certainly changes its _meaning_ - what it points to is
no longer valid.
(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());
)
--
Richard Herring
|