Vector Iterator not incrementable
May 25, 2013 at 3:22pm UTC
I am receiving the error: Vector Iterator not incrementable. However, when erasing I'm already re-setting 'it' and pre-incrementing at the end of the while-clause. Do you have any idea, what's wrong?
1 2 3 4 5 6 7 8 9 10
vector<st>::iterator it = map[0][0].begin();
while (it != map[0][0].end()) {
if ((*it).val == 5) {
try {
it = map[0][0].erase(it);
} catch (exception e) {}
}
++it;
}
May 25, 2013 at 3:37pm UTC
If it is a run-time error then its reason is that you are trying to increase the iterator equal to end().
The valid code will look like
1 2 3 4 5 6 7 8 9 10 11 12
vector<st>::iterator it = map[0][0].begin();
while (it != map[0][0].end()) {
if ((*it).val == 5) {
try {
it = map[0][0].erase(it);
} catch (exception e) {}
}
else
{
++it;
}
}
Last edited on May 25, 2013 at 3:38pm UTC
Topic archived. No new replies allowed.