Vector Iterator not incrementable

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;
}
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
Topic archived. No new replies allowed.