PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > STL Vector Handle
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
STL Vector Handle

Réponse
 
LinkBack Outils de la discussion
Vieux 30/12/2007, 00h59   #1
Mark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut STL Vector Handle

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).
  Réponse avec citation
Vieux 30/12/2007, 01h10   #2
Pascal Bourguignon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: STL Vector Handle

Mark <mnbayazit@gmail.com> writes:

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


Read the doc of vector::end.

It doesn't return an iterator pointing to the last element of the
sequence, but one step beyond.

In the case of a vector, you're lucky, you can use button.end()-1,
assuming you could add other elements before erasing it. Otherwise it
would be simplier to just use pop_back.
http://www.cppreference.com/cppvector/index.html


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


Beware that if you insert or remove elements in the container, the
iterators you got before may (and for some class of container will)
become invalid.


--
__Pascal Bourguignon__ http://www.informatimago.com/

"This statement is false." In Lisp: (defun Q () (eq nil (Q)))
  Réponse avec citation
Vieux 30/12/2007, 01h13   #3
jkherciueh@gmx.net
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: STL Vector Handle

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
  Réponse avec citation
Vieux 30/12/2007, 01h40   #4
Daniel T.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: STL Vector Handle

Mark <mnbayazit@gmail.com> 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();


buttons.end() doesn't point to the object you just pushed in. Try 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?


Use a std::list instead. The iterators aren't invalidated with it.
  Réponse avec citation
Vieux 30/12/2007, 01h56   #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
Vieux 30/12/2007, 07h29   #6
Salt_Peter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: STL Vector Handle

On Dec 29, 8:56 pm, Mark <mnbaya...@gmail.com> wrote:
> 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!!



Have you considered std::list's member function back() ?

  Réponse avec citation
Vieux 30/12/2007, 11h14   #7
Mark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: STL Vector Handle

On Dec 29, 11:29 pm, Salt_Peter <pj_h...@yahoo.com> wrote:
> On Dec 29, 8:56 pm, Mark <mnbaya...@gmail.com> wrote:
>
>
>
> > 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!!

>
> Have you considered std::list's member function back() ?


You mean to erase from the back? The sample I posted might be a little
misleading. The "button" is at the back when it's pushed onto the
list, but not necessarily when I delete it (it could have moved into
the middle somewhere).
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h36.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18037 seconds with 15 queries