Hello all, I'm a bit stuck with this one.
I have a list that holds "Points," a class I have created. Some of these points are designated as "loop1 points" (the beginning point of a loop) and some as "loop2 points" (the end of a loop).
I have a RemovePoint() method which works when removing individually selected points. However, if the selected point is a loop1 point, I would like it to remove that point as well as all the other points between it and including the next loop2 point that shows up. What I have compiles but crashes at runtime.
On the first glance the offending loop dereferences the iterator before testing if it is end(), double-advances loopItr on erase, and trashes itr.
But why use so many loops anyway?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// scan for points with the desired id
for(auto i = find_if(PointList.begin(), PointList.end(), HasId(id));
i != PointList.end();
i = find_if(i, PointList.end(), HasId(id)) )
{
if(i->GetType() == "loop1") {
// scan for the next loop2
auto loopItr = find_if(i, PointList.end(), HasType("loop2"));
if(loopItr != PointList.end()) {
++loopItr; // include the loop2 in the range
i = PointList.erase(i, loopItr); // erase from now to loop2
continue;
}
} // end if loop1
++i;
} // end scan for id
where HasType and HasId are either lambdas such as