Consider the loop condition when students has an initial size of 1 and the first (and only) element in the array matches the condition. In that case, one element is removed from the vector in the body of the loop and the for counter is incremented at the conclusion of the loop body. So, for the second iteration of the loop, students.size() evalutes to 0 and it is 1. Since 1 is not equal to 0, the loop continues, and students.at is invoked with an invalid index.