Re: vector.erase(iterator iter) will change "iter" or not?
On Feb 22, 8:31 am, 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!
I'm not sure whether you can use the word "changed" here or
not. After the erase, iter has no value. At least, not one you
can legally access.
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;
Not with the implementation I use After the erase, the only way
you could look at the representation is by means of a memcpy,
and if you do:
unsigned char before[ sizeof( std::vector<int>::iterator ] ;
memcpy( before, &iter, sizeof( std::vector<int>::iterator ) ) ;
v.erase( iter ) ;
memcmp( before, &iter, sizeof( std::vector<int>::iterator ) ) ;
the memcmp will return a value not equal to 0.
And trying to use the iterator will cause a core dump. (To be
very, very clear:
#include <vector>
int
main()
{
static int const init[] = { 1, 2, 3, 4, 5 } ;
std::vector< int > v( init, init + 5 ) ;
std::vector< int >::iterator
iter = v.begin() + 3 ;
v.erase( iter ) ;
std::vector< int >::iterator
i2 = iter ;
}
core dumps on the last line in main. You cannot read an invalid
iterator other than as an array of bytes.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34