i have the following problem ahead of me: I have a set containing pointers to vertices. If two vertices are similar, i want to delete one of them out of the set.
I do this check via nested for loop:
set<Vertex*> vertexSet;
set<Vertex*> vertexSet2;
for(...) { // outer loop
... //vertexSet gets filled externally
vertexSet2 = vertexSet; //copying vertexSet, to not delete from it, while iterating over it.
for(it = vertexSet.begin(); it != vertexSet.end(); ++it) {
for(jt = next(it); jt != vertexSet.end(); ++jt) {
if((*it)->value == (*jt)->value) { //check if similar/equal
vertexSet2.erase(jt); //erase the similar vertex out of the second set
}
}
}
vertexSet2.clear();
vertexSet.clear(); //clear both sets for next outer loop iteration
} //end of outer loop
This works just fine for the first iteration of the outerloop. The problem is, when the program reaches the step of copying the content of vertexSet to vertexSet2 the second time, it crashes.
I don't get that, because i'm clearing both sets and they both have size() == 0 afterwards.
Any ideas what the problem could be?
EDIT: I now changed my code accordingly. I am using only 1 set and changed the point where i increment my jt iterator.
set<Vertex*> vertexSet;
for(...) { // outer loop
... //vertexSet gets filled externally
for(it = vertexSet.begin(); it != vertexSet.end(); ++it) {
for(jt = next(it); jt != vertexSet.end();) { //REMOVED INCREMENT OF JT HERE
set<Vertex*>::iterator temp = jt++; //AND MOVED IT HERE
if((*it)->value == (*temp)->value) { //check if similar/equal
vertexSet.erase(temp); //erase the similar vertex out of the second set
}
}
}
vertexSet.clear(); //clear set
} //end of outer loop