Here's my code:
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
v.push_back(6);
v.push_back(7);
for (vector<int>::iterator it = v.begin(); it != v.end(); ++it)
{
if (*it == 7)
v.erase(it);
cout << *it << endl;
}
The issue is, the for
loop does not stop running and prints garbage values. Can you give some insight on the issue...like maybe the last element stores information about size or the address of the end()
?
Using the erase()
method works fine for other locations in the vector, except the last element.
The console log shows garbage values like this:
34603778
35652354
50397954
34603592
34603536
34603536
34603536
34603536
34603794
36700688
34603536
34603536
34865684
51511824
34603536
34865680