Ich habe eine Std :: Liste, die ich auf der Grundlage einiger Berechnungen zu sortieren versuchen. Point2D ist eine Struktur mit nur int no, double x und double y;Benutzerdefinierte list.sort Vergleiche in C++
Hier ist die Methode, die meine List.Sort Code enthält:
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;
}
Und hier ist die compare_points Methode, die ich schrieb:
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;
}
}
}
Der Fehler Visual Studio ist bei mir spuckt ist „Graham Scan: : compare_points ": Nicht-Standardsyntax; Verwendung ‚&‘ ein Zeiger auf ein Element erstellen“
Ich habe nicht viel Erfahrung in C haben ++, aber ich versuche, einige Java-Code zu konvertieren, die eine TreeSet zu C++ verwendet und dies ist mein Versuch.
Jede Hilfe würde geschätzt
http://stackoverflow.com/a/4288479/212870 –
die Vergleichsfunktion Stellen 'static' und versuche es erneut. Aber lassen Sie mich vor der * strict-weak-order * -Anforderung für die Vergleichsfunktion warnen. Ihre Implementierung ist komplex und es ist schwer zu sagen, ob sie dieser Anforderung entspricht. – PaulMcKenzie