私はいくつかの計算に基づいてソートしようとしているstd :: listを持っています。 Point2Dはint no、double x、およびdouble yのみを持つ構造体です。ここでC++でのカスタムリストソートの比較
は、私はlist.sortコードが含まれている方法です。
std::vector<Point2D> GrahamScan::getSortedPointSet(std::vector<Point2D> points) {
Point2D lowest = getLowestPoint(points);
std::list<Point2D> list;
for (int i = 0; i < (int)points.size(); i++) {
list.push_back(points[i]);
}
list.sort(compare_points);
std::vector<Point2D> temp;
for (int i = 0; i < (int)list.size(); i++) {
temp.push_back(list.front());
list.pop_front();
}
return temp;
}
そして、ここで私が書いたcompare_points方法です:
bool GrahamScan::compare_points(const Point2D& a, const Point2D& b) {
if (a.x == b.x && a.y == b.y) {
return false;
}
double thetaA = atan2((long)a.y - lowest.y, (long)a.x - lowest.x);
double thetaB = atan2((long)b.y - lowest.y, (long)b.x - lowest.x);
if (thetaA < thetaB) {
return false;
}
else if (thetaA > thetaB) {
return true;
}
else {
double distanceA = sqrt((((long)lowest.x - a.x) * ((long)lowest.x - a.x)) +
(((long)lowest.y - a.y) * ((long)lowest.y - a.y)));
double distanceB = sqrt((((long)lowest.x - b.x) * ((long)lowest.x - b.x)) +
(((long)lowest.y - b.y) * ((long)lowest.y - b.y)));
if (distanceA < distanceB) {
return false;
}
else {
return true;
}
}
}
をVisual Studioは私に吐き出しているエラーは「GrahamScanですが。 :compare_points ":非標準構文。私はC++での多くの経験を持っていないが、私はC++にTreeSetのを使用しており、これが私の試みである、いくつかのJavaコードを変換しようとしている「のメンバーに
をポインタを作成するために、「&」を使用しています。
どのような援助をいただければ幸いです
http://stackoverflow.com/a/4288479/212870 –
比較関数を作ります'static'を実行して再試行してください。しかし、比較関数の* strict-weak-order *要件を警告してくれます。あなたの実装は複雑で、この要件に従うかどうかは分かりません。 – PaulMcKenzie