我不熟悉 C++ 中的 OO。
我一直在将 MyPoint
类的实例推送到
vector <MyPoint> trianglePoints;
像这样:
trianglePoints.push_back(MyPoint(x,y));
这是我对 MyPoint 的定义:
class MyPoint {
public:
float x;
float y;
MyPoint(float x, float y) //constructor
{
this->x=x;
this->y=y;
}
}; //end
将三个点插入 vector 后,我调用一个函数来渲染三角形,然后执行以下操作:
trianglePoints.clear();
问题:
a) 如何从 vector 中获取三个 x,y 坐标?我想将每个存储到自己的 int xi,yi 中以呈现它们。
b) 即使我没有为 MyPoint 类定义析构函数,也可以在 vector 上调用 clear()
吗?
最佳答案
一)
trianglePoints[0].x
trianglePoints[0].y
trianglePoints[1].x
trianglePoints[1].y
trianglePoints[2].x
trianglePoints[2].y
二)
是的,该类使用默认析构函数。
关于c++ - 从 vector 中正确访问对象的属性并将其清除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3947127/