I want to iterate all the elements of a vector and for each element to check a condition for all other elements of the vector.
The logic:
Precondition: q is not in vector for every x,y in vector if d(x, y) <= d(x, q) && d(x, q) <= d(y, q) then eliminate x, y An approach:
for(vector<Point_d>::iterator it = candidates.begin(); it != candidates.end(); ++it){ for (vector<Point_d>::iterator it2 = candidates.begin(); it2 != candidates.end(); ++it2) { if(dist.transformed_distance(*it, *it2) <= dist.transformed_distance(*it, q) && dist.transformed_distance(*it, q) <= dist.transformed_distance(*it2, q)){ /* Remove x,y without invalidate the iterators */ } } } I know that if I remove an element inside the loop the iterators will be invalidated. Is there any way to do this with Erase-Remove Idiom or is there any other way to do this? I have searched a lot and I have find various piece that I can combine to make it work, ie by remove the iterators from for-loops and use erase and remove_if, but I can't figure it out and also because I am new to c++ and STL I would like to hear better approaches.
EDIT
Also if is possible I don't want to do the condition for the same elements ie d(x, x) <= d(x, q).
it != it2; and secondly, store somewhere in list for example elements that should be remove, and after iterate over vector, delete it, instead of deleting it directly in iterationvector, why not use index instead of iterator?vec.erase(vec.begin() + 1)