the runtime error happens at the below line. Could you explain why the error happens? Can not I increase iter2 after erase the object iter2 pointed out previously? iter2++; //// error happened
Best regards,
#include<iostream>
#include<vector>
using namespace std;
int main(void) {
vector <string> v;
v.push_back("aaaa");
v.push_back("bbbb");
v.push_back("cccc");
v.push_back("dddd");
v.push_back("eeee");
v.push_back("ffff");
vector<string>::iterator iter2;
iter2 = v.begin() + 2;
v.erase(iter2);
iter2++; //// error happened
for (iter2 = v.begin(); iter2 != v.end(); iter2++) {
cout << *iter2 << " ";
}
}
Erase function has return value - iterator pointing to the next object in the container. So just use it like this:
iter2=erase(iter2);
Now you can increment it all you want.