我是c ++的新手,我正在尝试计算Point(x,y,z)集的凸包C++
.
我在main方法中调用了以下方法:
vector<Point> convexHull(Point Points[], int n) {
vector<Point> v;
// Find the bottommost Point
int ymin = Points[0].getY();
int min = 0;
for (int i = 1; i < n; i++) {
int y = Points[i].getY();
// Pick the bottom-most or chose the left most Point in case of tie
if ((y < ymin) || (ymin == y && Points[i].getX() < Points[min].getX()))
ymin = Points[i].getY(), min = i;
}
// Place the bottom-most Point at first position
Points[min] = Points[0].swap(Points[min]);
// Sort n-1 Points with respect to the first Point. A Point p1 comes
// before p2 in sorted ouput if p2 has larger polar angle (in
// counterclockwise direction) than p1
p0 = Points[0];
qsort(&Points[1], n - 1, sizeof (Point), compare);
// Create an empty stack and push first three Points to it.
stack<Point> S; // on debug the project I find that the problem is here
S.push(Points[0]);
S.push(Points[1]);
S.push(Points[2]);
// Process remaining n-3 Points
for (int i = 3; i < n; i++) {
// Keep removing top while the angle formed by Points next-to-top,
// top, and Points[i] makes a non-left turn
while (orientation(nextToTop(S), S.top(), Points[i]) != 2)
S.pop();
S.push(Points[i]);
}
// Now stack has the output Points, print contents of stack
while (!S.empty()) {
Point p = S.top();
cout << "(" << p.getX() << ", " << p.getY() << ", " << p.getZ() << ")" << endl;
v.push_back(Point(p.getX(), p.getY(), 0));
S.pop();
}
return v;
}
Run Code Online (Sandbox Code Playgroud)
它给出了这个错误:
*** glibc detected *** /home/user/NetBeansProjects/DGILOG-ask/dist/Debug/GNU-Linux-x86/dgilog-task: malloc(): memory corruption (fast): 0x08de1238 ***
Run Code Online (Sandbox Code Playgroud)
我在网上搜索了同样的错误,但我不知道该怎么办.
Point.cpp
#include <iostream>
#include <math.h>
#include <ostream>
using namespace std;
#include "Point.h"
Point::Point() : x(0), y(0), z(0) {
}
Point::Point(ostream &strm) {
strm << "Type the abscissa: ", cin >> this->x;
strm << "Type the ordinate: ", cin >> this->y;
strm << "Type the applicate: ", cin >> this->z;
}
Point::Point(float x, float y, float z) : x(x), y(y), z(z) {
}
/**
* Destructor
*/
Point::~Point() {
}
//Other methods
float Point::dist2D(Point &other) {
float xd = x - other.x;
float yd = y - other.y;
return sqrt(xd * xd + yd * yd);
}
float Point::dist3D(Point &other) {
float xd = x - other.x;
float yd = y - other.y;
float zd = z - other.z;
return sqrt(xd * xd + yd * yd + zd * zd);
}
Point Point::swap(Point p) {
Point aux(x, y, z);
x = p.x;
y = p.y;
z = p.z;
return aux;
}
void Point::print(ostream &strm) {
strm << "Point(" << this->x << "," << this->y << "," << this->z << ")" << endl;
}
bool Point::operator<(const Point &p) const {
return x < p.x || (x == p.x && y < p.y);
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
由于您没有发布完整的程序,因此您需要注意以下事项:
convexHull(Point Points[], int n)
在该函数中没有任何地方检查n是否在Points数组的范围内.你应该在整个函数中使用向量.例如:
int ymin = Points[0].getY();
int min = 0;
for (int i = 1; i < n; i++) {
int y = Points[i].getY();
Run Code Online (Sandbox Code Playgroud)
如果我将NULL指针作为第一个参数(或者甚至是无效指针)传递,或者如果n太大,则会出现访问冲突.使用向量可以大大减少或彻底消除这些问题.使用vector,您可以使用size()成员函数进行完整性测试,以确保Point具有相关的条目数.现在,没有办法在你的函数中进行这样的测试.
下一期:
S.push(Points[0]);
S.push(Points[1]);
S.push(Points[2]);
Run Code Online (Sandbox Code Playgroud)
你怎么知道至少有3个条目?您不知道,并且该功能无法检查.你所拥有的只是一个被传递的指针,以及一些任意数字n.如果您正在使用C++,那么您就不应该习惯于以类似"C"的方式编写代码.你有矢量,所以使用它有利.
下一期:
qsort(&Points[1], n - 1, sizeof (Point), compare);
Run Code Online (Sandbox Code Playgroud)
由于您没有发布Point是什么,如果Points是非POD类型,则qsort()的这种用法会导致未定义的行为.
停止使用qsort().在C++程序中使用qsort()表明编码器要么是1)使用他们习惯的C程序员(通常遵循令人惊讶的意外重现),要么是2)新手C++程序员阅读C书或程序作为编写适当的C++程序的指导.
使用std :: sort() - 您正在编写C++应用程序,而不是C应用程序.std :: sort是类型安全的,更易于使用和设置,适用于遵循严格弱顺序的POD和非POD类型.
归档时间: |
|
查看次数: |
24990 次 |
最近记录: |