【发布时间】:2012-11-05 21:38:12
【问题描述】:
我有一个标题,它由不同的模板函数组成
#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。如何使用标题中的模板函数lessThan 和greaterThan 对p2dL 进行排序?即根据x 或y 值对列表进行排序。
编辑:因此,根据 Anton 的评论,我想出了这个:
bool Point2D::operator<(Point2D p2d) {
if (this->x < p2d.x || this->y < p2d.y
|| this->distFrOrigin < p2d.distFrOrigin) {
return true;
}
else {
return false;
}
}
我做对了吗?
【问题讨论】:
-
你需要为你的类实现
-
没有“正确”的方式来订购两个 2D 点。您必须决定哪个任意选择适合您的问题。
标签: c++ list function templates sorting