【发布时间】:2017-01-24 06:37:46
【问题描述】:
鉴于以下
class Coordinate {
int i,j;
};
list<shared_ptr<Coordinate>> coordinates;
(Coordinate 只是我的代码真正包含的更简单的版本。)我想根据到给定坐标iCenter,jCenter 的最小欧几里得距离对list 进行排序。最接近此坐标的Coordinate 应该在列表coordinates 中的第一个位置。此外,我想使用以下结构
struct ComparatorForCoordinate {
bool operator() (const shared_ptr<Coordinate>& c1, const shared_ptr<Coordinate>& c2) {
// return distance(c1 to center) > distance(c2 to center)
}
};
// sorting:
coordinates.sort(ComparatorForCoordinate());
我的问题是我的operator() 需要知道坐标iCenter,jCenter。如何将这两个双精度值传递给比较函数?天真地试图让操作员另外有两个中心位置的双参数是行不通的。如果可以的话,这个电话对我来说会很棒:
coordinates.sort(ComparatorForCoordinate(iCenter,jCenter));
【问题讨论】:
-
我调整了标题,并添加了 STL 标签,希望其他用户更容易找到这个问题(和答案)。如果您不喜欢这些更改,请随时恢复。