STL Vector Handle

M

Mark

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

Pascal Bourguignon

Mark said:
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.
 
J

jkherciueh

Mark said:
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
 
D

Daniel T.

Mark said:
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.
 
M

Mark

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!!
 
S

Salt_Peter

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() ?
 
M

Mark

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,536
Latest member
VeldaYoung

Latest Threads

Top