Yeah, but from what I saw, it turns out that
std::list::erase(..) returns void
which I found here:
http://www.sgi.com/tech/stl/List.html
and here:
http://www.msoe.edu/eecs/ce/courseinfo/stl/list.htm
So I don't see how I could do:
iter = bots.remove(iter);
since the std::list::erase(const &T) doesn't return anything.
Quote:Original post by Sagar_Indurkhya
Yeah, but from what I saw, it turns out that
std::list::erase(..) returns void
which I found here:
http://www.sgi.com/tech/stl/List.html
Your reading it wrong, it takes an iterator and returns an iterator, look a again.
Quote:Original post by Sagar_Indurkhya
and here:
http://www.msoe.edu/eecs/ce/courseinfo/stl/list.htm
That reference is incorrect or out of date.
Quote:Original post by Sagar_Indurkhya
since the std::list::erase(const &T) doesn't return anything.
Well that is not even the correct signature, std::list::erase takes iterators (has two overloads) and returns an iterator.
Quote:Original post by Sagar_Indurkhya
it turns out that I can't do this:
iter = bots.remove(iter); on a list
instead I have to do something like:
list<Bot>::iterator i = --iter;
bots.remove(++iter);
iter = i;
Don't do that... do this:
if(remove the bot) bots.remove(iter++);else ++iter;
If you look at the source for the iterator, you see that the post-increment operator looks like this:
iterator operator++(int){ iterator _Tmp = *this; ++*this; return (_Tmp); }
Whereas the pre-increment operator is:
iterator& operator++(){ _Ptr = _Acc::_Next(_Ptr); return (*this); }
So, iter++ creates a copy of itself, then calls it's own pre-incrementer, and then returns the un-incremented copy. So it's perfectly safe to remove/erase an element out of a STL container using it... the original iterator you're using in your list will still be valid.
And if you just want to increment an iterator outside of an erase/remove, just use ++iter, and save the copy.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement