c++ - How to erase a vector element within double for loop -
i have vector of struct
vector<contour> hotspots;
contour structure have defined below
struct contour { double arc; double area; point2f center; double me; double hullarea; vector<point> contourvector; vector<point> hullvector; bool operator <(const contour& comp) { return me < comp.me; } };
so trying loop through , remove every contour contained within contour, can working part index error , assuming has when erase element index's messed go out of range. here loop wrote:
vector<vector<point>> cleanup(vector<point2f> centers, vector<moments> areas, vector<vector<point>> contours) { (int = 0; < contours.size(); i++) (int j = 0; j < contours.size(); j++) if (i != j) { if ((areas[i].m00 < areas[j].m00) == true && (pointpolygontest(contours[j], centers[i], false) > 0) == true) { contours.erase(contours.begin() + i); centers.erase(centers.begin() + i); areas.erase(areas.begin() + i); } } return contours; }
i have tried few different solutions such - 1 deletes wrong contour, , instead of using erase, overwriting element want delete last 1 using .pop_back delete last element did not seem work either. ideas?
edit: fixed out of sync issue areas , centers, issue shows right away call function erase of contours.
you can keep list of indices items delete , delete items @ indices @ end.
vector<vector<point>> cleanup(vector<point2f> centers, vector<moments> areas, vector<vector<point>> contours) { std::<int> itemstoerase; (int = 0; < contours.size(); i++) (int j = 0; j < contours.size(); j++) if (i != j) { if ((areas[i].m00 < areas[j].m00) == true && (pointpolygontest(contours[j], centers[i], false) > 0) == true) { itemstoerase.push_front(i); } } while ( !itemstoerase.empty() ) { int = itemstoerase.back(); contours.erase(contours.begin() + i); centers.erase(centers.begin() + i); areas.erase(areas.begin() + i); itemstoerase.pop_back(); } return contours; }
Comments
Post a Comment