I'm writing a program to calculate the perimeter of a convex hull using the graham scan and need to find the lowest y coordinate in a set of data points. I'm using std::min_element(vector.begin(), vector.end())
with an overloaded <
operator in my struct point
. The problem is that some points may share the same lowest y coordinate, and in that case I need to compare them using their x values. Is there any quick cheat to check if any other points are sharing the same y as the min_element without having to loop through everything?
struct:
struct cord{
cord():x(0),y(0){}
int x,y;
bool operator<(const cord &p) const { return y < p.y; }
};
typedef std::vector<cord>::iterator vecIter;
function call:
vecIter lowestPoint =
std::min_element(points.begin(), points.end());
std::cout << "the lowest point of the data set is: " << "(" <<
lowestPoint->x << "," << lowestPoint->y << ")" << std::endl;
So, just something like this? (to replace your existing operator<
function)
bool operator<(const cord &p) const
{
if (y != p.y)
return y < p.y;
else
return x < p.x;
}