-4

I am trying reset an iterator after it has been erased by going

while (vBegin != vEnd)
{ 
    //some stuff; 
    vectorThing.erase(vbegin); 
    vbegin = vectorThing.begin();
    vEnd = vectorThing.end();
}

This causes vEnd to equal the thing I just deleted, how do I get it to point to the very end like it was before?

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
randomname
  • 265
  • 1
  • 9
  • 20
  • 1
    `This causes vEnd to equal the thing I just deleted` No it does not. Whatever gave you this idea? – Igor Tandetnik Mar 07 '15 at 04:17
  • I went into a debugger and it did that – randomname Mar 07 '15 at 04:18
  • 1
    What, if any, is the relationship between `vbegin` and `vBegin`? – Igor Tandetnik Mar 07 '15 at 04:18
  • @randomname Is there a reason why you didn't use the `erase/remove_if` idiom instead of erasing while you're iterating over a vector? http://stackoverflow.com/questions/347441/erasing-elements-from-a-vector – PaulMcKenzie Mar 07 '15 at 04:18
  • `container.end()` will give you an iterator to *one beyond then end* of the container. See e.g. the figure in [this reference for `std::end`](http://en.cppreference.com/w/cpp/iterator/end). – Some programmer dude Mar 07 '15 at 04:19
  • The first thing you need to do is to _slow down_. Stop making silly mistakes. I can spot three obvious typos in this incomplete snippet alone. Only once you apply some rigour to your work will you be able to solve these problems. – Lightness Races in Orbit Mar 07 '15 at 04:20
  • Ok im making video of it – randomname Mar 07 '15 at 04:26
  • @randomname `I am trying reset an iterator after it has been erased by going` Something to consider -- A lot of these issues with erasure can be eliminated if your problem can be expressed as simply iterating over the entire vector, mark the items that should be erased without really erasing them, and then at the end of the loop, actually erase those items that were "marked". If you think of your problem in these terms, then you avoid all of this mess by using the erase/remove idiom that was already mentioned. – PaulMcKenzie Mar 07 '15 at 04:27
  • http://recordit.co/WCc8bHnvYd I am using VS12 – randomname Mar 07 '15 at 04:27
  • Will it be a video of your real code, cause a video of the compile errors from `vectorThing.being();` isn't all that exciting. – Retired Ninja Mar 07 '15 at 04:28
  • what text editor / IDE is that? – user4578093 Mar 07 '15 at 04:29
  • 2
    [This seems to work](http://ideone.com/vJLIpz), perhaps you should show us the real code. – Retired Ninja Mar 07 '15 at 04:35
  • I think I found my error but am not sure how to fix it: http://en.cppreference.com/w/cpp/container/vector/erase It says "Invalidates iterators and references at or after the point of the erase, including the end() iterator." I think my iterators were invalidated...not sure how to fix it though. – randomname Mar 07 '15 at 06:20
  • `erase` returns an iterator to the current position after the erase happens, or you can just reinitialize it as you showed in your example and as I showed in the live example. If you're still having trouble I suspect it is in the code you have not shown, so post a complete example. – Retired Ninja Mar 07 '15 at 08:27

2 Answers2

0

Do you know if your container is empty? If it is, then the end() function returns the same iterator value as the vector::begin(). Perhaps that is what you are seeing? Such as if your container size was 1 and you just deleted your first element, then you would be stuck with the same begin and end element.

Pita
  • 111
  • 7
0

Try the following way

while ( vBegin != vectorThing.end() )
{ 
    //some stuff; 
    vBegin = vectorThing.erase(vbegin); 
}

or

while ( vBegin != vEnd )
{ 
    //some stuff; 
    vBegin = vectorThing.erase(vbegin); 
    vEnd = vectorThing.end(); 
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335