Discussion: STL Vector Handle
Afficher un message
Vieux 30/12/2007, 02h56   #5
Mark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: STL Vector Handle

On Dec 29, 5:13 pm, jkherci...@gmx.net wrote:
> Mark wrote:
> > I need a handle to an element in a vector so that I can erase it when
> > I'm done with it. I tried doing this:

>
> > buttons.push_back(this);
> > iter = buttons.end();

>
> > And then when I'm done with it, I did this:

>
> > buttons.erase(iter);

>
> > But my program just crashes. I don't think I'm allowed to store
> > iterators like that, since the memory location might change. I can't
> > store the index into the vector either, because that might change as
> > well. What can I do?

>
> > Or, what other container types might be suitable that WOULD allow me
> > to do this? I need a resizeable container type that I can quickly
> > iterate over, and insert into (anywhere, doesn't matter) and remove
> > from (need handle for this).

>
> Sounds like std::list.
>
> Insertions into a list do not invalidate iterators. Erasing an element only
> invalidates the iterators pointing to that item. Insert and erase anywhere
> are constant time.
>
> Best
>
> Kai-Uwe Bux


I changed it to a list and fixed the end() problem (oops, I should
have known that). Works perfectly now, thanks!!
  Réponse avec citation
 
Page generated in 0,05188 seconds with 9 queries