//eliminate the child from the list
map<string, int> newpar = parents;
string chil = child;
for(map<string, int>::iterator it = newpar.begin(); it != newpar.end();)
{
//(it == newpar.end()) ALWAYS equals false. This is the problem!!
if(it->first == chil) //this is where it stops
{
//(it == newpar.end()) is false here... always...
newpar.erase(it);
//loop back without adding the next one
//(it == newpar.end()) is false here... always...
continue;
}
//only add if we didn't erase anything
//(it == newpar.end()) is false here... always...
/* WTF...WTFWTFWTFWTF?!?!?!?!*/
it++;
}
Will someone help me? I can not figure out why it is throwing segfaults.
Explanation (reason of emplimentation):
The following is supposed to allow for quick search of a parent path. Since the child path is also included within the list, it must be eliminated before we start searching.
When you erase an element from a map it invalidates the current iterator (it++ will yield meaningless results.) map::erase returns an iterator pointing to the element that follows the last removed element.
this should fix your problem:
My understanding is that a vector<string>::iterator would be able to function correctly in this loop. Is this strictly a map<class, class>::iterator feature?