Idiomatic STL: Iterating over a list and inserting elements
- by mkilling
I'm writing an algorithm that iterates over a list of points, calculates the distance between them and inserts additional points if the distance is too great. However I seem to be lacking the proper familiarity with STL to come up with an elegant solution. I'm hoping that I can learn something, so I'll just show you my code. You might have some hints for me.
for (std::list<PathPoint>::iterator it = ++points_.begin();
it != points_.end(); it++)
{
Vector curPos = it->getPosition();
Vector prevPos = (--it)->getPosition();
Vector vecFromPrev = curPos - prevPos;
float distance = vecFromPrev.abs();
it++;
if (distance > MAX_DISTANCE_BETWEEN_POINTS)
{
int pointsToInsert = (int)(distance / MAX_DISTANCE_BETWEEN_POINTS);
Vector curPos = prevPos;
for (int i = 0; i < pointsToInsert; i++)
{
curPos += vecFromPrev / pointsToInsert;
it = points_.insert(it, PathPoint(curPos, false));
it++;
}
}
}