Today, using the Vector.erase () method, found a lot of ghost animals. Later, the data was found, erase () is to delete the current iterator to return the next iterator. If you just use erase but do not update the iterated iterator will cause the current iterator to be released into a wild pointer, and then use + + after the wild pointer-the operation will certainly be problematic. So you need to update the iterator, ITER = Myvec.erase (); After the update, ITER has pointed to the next bit of the deleted element. At this point out of judgment, and back to the for loop, the for loop ITER and + + once, so that the latter element is skipped. So, after the use of ITER = Myvec.erase (), a decisive and clever--iter is needed to make ITER return to its proper position.
There is no solution to the problem, and how the length of the vector changes when erase is used. When erase is used, the elements behind the vector should be moved forward. The length should be the original length-1. Specifically want to know the results, write a short program to verify your own OK.
Stl--vector.erase () Usage