Here's a hint: NEVER use for loops for iterating over a vector if you plan on deleting elements. Sure, you can do it just as easily, but "blind-index-incrementing" is dangerous if you don't know how vectors work. A while loop and manual incrementing gives you far more control and forces you to think on its behavior.
An example: the vector of ints V{8, 4, 8, 8, 9}, and you wish to delete all 8's
i = 0: V[0] = 8 -> erase -> {4, 8, 8, 9}, ++i.
i = 1: V[1] = 8 -> erase -> {4, 8, 9}, ++i.
i = 2: V[2] = 9 -> skip, ++i.
i = 3 = V.size() -> end. |
If you delete element X, all elements ]X, size[ are copied to [X, size-1[. Due to your auto-incrementing, you skipped several elements. Not a disaster, but unexpected behavior nontheless.
Now, try the example above again with the vector {4, 4, 4, 8} and see if you can figure out why your problem crashes. Hint: the problem is your for's stopping condition.