Re: STL vector and push_back
- From: Tom Plunket <gamedev@xxxxxxxxx>
- Date: Thu, 29 Jun 2006 18:28:00 -0700
Abstract Dissonance wrote:
I'm working with STL vector and trying to get in the habit of using
iterators to work through the vector instead of "direct indexing". It
doesn't seem all that difficult as they seem to work like pointers but I'm
having a problem when I create an iterator for a vector then push something
onto the end of vector with push_back it will "destory" my iterator(I'm not
sure exactly what happens but the iterator becomes useless... not sure if it
gets set to s.end() or what).
Yes, it is defined to work that way. The iterator is implemented as a
pointer to the element, push_back goes and reallocates your memory,
and your pointer no longer points to anything useful.
In this case, you're stuck using an index or another data structure.
Iterators on std::list, for example, don't get invalidated by list
operations.
-tom!
.
- References:
- STL vector and push_back
- From: Abstract Dissonance
- STL vector and push_back
- Prev by Date: Re: ls lacking a feature?
- Next by Date: Re: ls lacking a feature?
- Previous by thread: STL vector and push_back
- Next by thread: Re: STL vector and push_back
- Index(es):
Relevant Pages
|