I have a header, which consists of different template functions
#include <cmath>
template<class T>
bool lessThan(T x, T y) {
return (x < y);
}
template<class T>
bool greaterThan(T x, T y) {
return (x > y);
}
A class
class Point2D {
public:
Point2D(int x, int y);
protected:
int x;
int y;
double distFrOrigin;
In my driver class, I have an STL List of Point2D: list<Point2D> p2dL. How do I sort p2dL using the template functions lessThan and greaterThan in my header? i.e. Sort the list based on x or y value.
EDIT: And so, based on Anton's comment, I came up with this:
bool Point2D::operator<(Point2D p2d) {
if (this->x < p2d.x || this->y < p2d.y
|| this->distFrOrigin < p2d.distFrOrigin) {
return true;
}
else {
return false;
}
}
Did I do it correctly?