さまざまなテンプレート関数で構成されるヘッダーがあります
#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);
}
クラス
class Point2D {
public:
Point2D(int x, int y);
protected:
int x;
int y;
double distFrOrigin;
私のドライバークラスには、Point2DのSTLリストがありますlist<Point2D> p2dL
。p2dL
テンプレート関数lessThan
とgreaterThan
ヘッダーを使用して並べ替えるにはどうすればよいですか?x
つまり、またはy
値に基づいてリストを並べ替えます。
編集:そして、アントンのコメントに基づいて、私はこれを思いついた:
bool Point2D::operator<(Point2D p2d) {
if (this->x < p2d.x || this->y < p2d.y
|| this->distFrOrigin < p2d.distFrOrigin) {
return true;
}
else {
return false;
}
}
私はそれを正しくしましたか?