how to organize or sort a std::vector

前端 未结 3 1901
难免孤独
难免孤独 2021-01-06 14:56

I have a vector full of cv::Point and I want to organize this vector so, that the Point with the smallest x and y value should be the first and the last one should have the

3条回答
  •  感动是毒
    2021-01-06 15:24

    Let's just arbitrarily assume that you determine the value of a point by adding the x and y (big assumption). Sorting is a fairly simply process:

    bool sort (const cv::Point p1, const cv::Point p2) { return (p1.x + p1.y) < (p2.x + p2.y)); }
    
    //int main or where ever
    //assuming name of vector is myVector
    std::sort(myVector.begin(), myVector.end(), sort);
    

    Just change the sort method to illustrate how you want to sort

提交回复
热议问题