Fenglou Mao wrote:

> On Mon, 25 Jan 1999, Burak Serdar wrote:
>
> > After a vector insert operation, all iterators (pointers) which are pointing
> > to vector elements are invalid. This is how STL vector semantics are defined.
> > It is not a replacement for arrays. But of course, if you store references to
> > objects in a vector and keep the pointers to objects instead of pointers into
> > a vector, you will not loose anything.
> >
>
> It is not true, I used code below to test it:
>
> #include <iostream.h>
> #include <stl.h>
>
> int main(){
>         vector<long> v;
>         vector<long>::iterator it;
>
>         long i=0;
>         for(i=0;i<10;i++)v.push_back(i);
>         vector<long>::reference r=v[0];
>         cout<<r<<"\n";
>         for(i=10;i<100;i++)v.push_back(i);
>         cout<<r<<"\n";
>
>         return 0;
> }
>
> This is the result:
>
> 0
> 268678424
>
> The reference 'r' can not trace v[0].
>
> Sincerely Yours,
>
> FengLou Mao
> *******************************
> ADD:Mr. FengLou Mao
>     Peking University
>     BeiJing
>     P.R.China
> Tel:86-10-62751490
> Fax:86-10-62751725

  I think I was unclear. This was what I said. You cannot keep references to the
elements of a vector if you are inserting elements. What I said was, instead of
storing objects in the vector, you can store references to objects, and instead of
storing a reference to an element of the vector, you can store a reference to the
actual object, i.e.

vector<someclass&>  v;

and instead of storing vector<someclass&>::iterator reference to an element of the
vector, use a someclass& reference to the actual object. After an insert
operation, vector<someclass&>::iterator will be definitely destroyed.

Hope this helps, though it may not.

Reply via email to