Sign in to follow this  

stl vector erase

This topic is 4071 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to erase an element from a vector ( not using remove_if ) The following code appears to be working when stepping through in the debugger, but I'm not sure if it's allowed to set the iterator to point to v[-1] when erasing the first element (even if not accessing it through the iterator) ?
void testVectorErase (void)
{
  typedef std::vector<unsigned int> myVec;
  myVec v;

  v.push_back (1);
  v.push_back (2);
  v.push_back (3);

  for (std::vector<unsigned int>::iterator iter = v.begin(); iter!=v.end(); ++iter)
  {
    if (*iter==1)
      v.erase ((--iter)+1); 
  }
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Kitt3n
I want to erase an element from a vector ( not using remove_if )

The following code appears to be working when stepping through in the
debugger, but I'm not sure if it's allowed to set the iterator to point
to v[-1] when erasing the first element (even if not accessing it
through the iterator) ?

*** Source Snippet Removed ***


iter = v.erase(iter);

Share this post


Link to post
Share on other sites
Thanks for explaining one valid way to do that.

Still I would like to know if the posted code is valid, or
if it might cause undefined behaviour (because the iterator
is pointing to element v[-1] ?

With a pointer I know that you can point anywhere you want,
as long as you don't try to access teh data which is being
pointed to, so with a pointer I could point to element v[-1]
and increment it to point to the first valid element...
I'm just not sure if iterators will behave the same...

Thanks in advance!

Share this post


Link to post
Share on other sites
It's not valid.

"A vector's iterators are invalidated when its memory is reallocated. Additionally, inserting or deleting an element in the middle of a vector invalidates all iterators that point to elements following the insertion or deletion point."

erase returns an valid iterator for next element after the erase.

Share this post


Link to post
Share on other sites
Quote:

With a pointer I know that you can point anywhere you want,
as long as you don't try to access teh data which is being
pointed to, so with a pointer I could point to element v[-1]
and increment it to point to the first valid element...


That's actually not really strictly true. The behavior of performing addition or subtraction between a pointer that points to an array element and an integer such that pointer no longer points to either a valid array element or the past-the-end (one beyond the last element) value is undefined.

Pointers that don't point to arrays are considered pointers to arrays of a single element for the purpose of that clause in the standard (5.7.4 and 5.7.5).

So pointing anywhere but within allocated storage or one-beyond that allocated storage (including to the element referenced hypothetically by p[-1]) is undefined.

Quote:

Still I would like to know if the posted code is valid, or
if it might cause undefined behaviour (because the iterator
is pointing to element v[-1] ?

The behavior isn't defined, since in order to achieve it, you've violated the precondition of the prefix decrement operator (and all expressable ways of obtaining an iterator "one before" the first item are expressible in terms of operator--, according to the standard -- the relevant portions start at section 24 -- so there's no way to obtain such a value without violating the precondition).

Share this post


Link to post
Share on other sites

This topic is 4071 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this