I have a map that is defined in global memory. I repeat this, let's say I'm in the third element. now I call another function that generates its own iterator on the same map, however it can erase the 4th or 5th ... elements on the map. my question is when I return from this function and continue to iterate over the map (let me remind you that I am in the third element), can my iterator be invalid or safe?
Sorry, I can’t attach the code for very long.
thank
EDIT: my question is something like this:
map<string,string> mapi;
void er() {
mapi.erase("t");
}
int main() {
mapi.insert(pair<string,string>("w","a"));
mapi.insert(pair<string,string>("e","a"));
mapi.insert(pair<string,string>("r","a"));
mapi.insert(pair<string,string>("t","a"));
mapi.insert(pair<string,string>("A","a"));
mapi.insert(pair<string,string>("u","a"));
mapi.insert(pair<string,string>("C","a"));
map<string,string>::iterator it;
for (it=mapi.begin(); it!=mapi.end(); it++) {
cout << it->first << endl;
if (it->first=="t")
er();
}
}
in this case, I erase the same element - valgrind says an error. however, when I delete other elements, it seems to work fine.