Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   vector.erase(iterator iter) will change "iter" or not? (http://www.velocityreviews.com/forums/t593225-vector-erase-iterator-iter-will-change-iter-or-not.html)

thomas 02-21-2008 02:16 PM

vector.erase(iterator iter) will change "iter" or not?
 
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?

Victor Bazarov 02-21-2008 02:46 PM

Re: vector.erase(iterator iter) will change "iter" or not?
 
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.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



David Côme 02-21-2008 02:55 PM

Re: vector.erase(iterator iter) will change "iter" or not?
 
On Thu, 21 Feb 2008 15:16:11 +0100, thomas <FreshThomas@gmail.com> 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?


Do :

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

Richard Herring 02-21-2008 03:31 PM

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

thomas 02-21-2008 04:03 PM

Re: vector.erase(iterator iter) will change "iter" or not?
 

> (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?

Eric Pruneau 02-21-2008 04:58 PM

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



Richard Herring 02-21-2008 05:03 PM

Re: vector.erase(iterator iter) will change "iter" or not?
 
In message
<c69c72bd-a326-4bc5-b1ef-4dd03288d4f4@i29g2000prf.googlegroups.com>,
thomas <FreshThomas@gmail.com> writes
>
>> (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?


It's a C++ STL idiom.

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


No. It erases everything in the range defined by the _two_ iterators
being passed to it. And remove() copies the non-3 elements down to the
beginning of s, returning an iterator to the first element beyond them.

If you don't know this stuff, you'd do better to read a good book than
ask random questions here.

--
Richard Herring

Martin York 02-21-2008 05:45 PM

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).


Old Wolf 02-22-2008 07:31 AM

Re: vector.erase(iterator iter) will change "iter" or not?
 
On Feb 22, 4:31 am, Richard Herring <junk@[127.0.0.1]> wrote:
> In message <fpk2sg$5d...@news.datemas.de>, Victor Bazarov
> >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.


The value certainly is changed: previously it was
well-defined and now it is indeterminate! It doesn't
point anywhere; it's nonsensical to say that what
it points to is not valid. Perhaps you mean to say
that the representation isn't changed;

Triple-DES 02-22-2008 07:47 AM

Re: vector.erase(iterator iter) will change "iter" or not?
 
On 22 Feb, 08:31, Old Wolf <oldw...@inspire.net.nz> wrote:
> On Feb 22, 4:31 am, Richard Herring <junk@[127.0.0.1]> wrote:
>
> > In message <fpk2sg$5d...@news.datemas.de>, Victor Bazarov
> > >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.

>
> The value certainly is changed: previously it was
> well-defined and now it is indeterminate! It doesn't
> point anywhere; it's nonsensical to say that what
> it points to is not valid. *Perhaps you mean to say
> that the representation isn't changed;


That's an interesting point. Consider:
int * p = new int(441);
delete p;

Would you say that the last line changes the value of p?

DP


All times are GMT. The time now is 01:09 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.