Use the std :: list :: iterator variable to keep track of the closest point as you go through the list. When you get to the end of the list, it will contain the nearest point and can be used to delete an item.
void erase_closest_point(const list<Point>& pointList, const Point& point) { if (!pointList.empty()) { list<Point>::iterator closestPoint = pointList.begin(); float closestDistance = sqrt(pow(point.x - closestPoint->x, 2) + pow(point.y - closestPoint->y, 2));
Building a list of points within the radius of a given point is similar. Note that an empty radius list is passed to the function by reference. Adding points to the list by reference eliminates the need to copy all points when returning a vector by value.
void find_points_within_radius(vector<Point>& radiusListOutput, const list<Point>& pointList, const Point& center, float radius) {
Use a copy again if:
struct RadiusChecker { RadiusChecker(const Point& center, float radius) : center_(center), radius_(radius) {} bool operator()(const Point& p) { const float distance = sqrt(pow(center_.x - px, 2) + pow(center_.y - py, 2)); return distance < radius_; } private: const Point& center_; float radius_; }; void find_points_within_radius(vector<Point>& radiusListOutput, const list<Point>& pointList, const Point& center, float radius) { radiusListOutput.reserve(pointList.size()); remove_copy_if(pointList.begin(), pointList.end(), radiusListOutput.begin(), RadiusChecker(center, radius)); }
Note that sqrt can be removed if you need extra performance, as the squared value works just as well for these comparisons. Also, if you really want to improve performance, than consider a data structure that allows you to split scenes like quadtree . The first issue is closely related to conflict detection , and there is a lot of useful information about this topic.