The following code initializes an STL vector of integers to
then iterates to erase all elements with value 1. The code compiles
but on running it we get a segmentation fault abort. Find and fix the
bug.
// this program is buggy!
#include<vector>
int main(int argc, char** argv) {
using namespace std;
int ret = 0;
vector<int> theVector;
theVector.push_back(2);
theVector.push_back(1);
vector<int>::iterator vi;
for(vi = theVector.begin(); vi != theVector.end(); vi++) {
if (*vi == 1) {
theVector.erase(vi);
}
}
return ret;
}