【发布时间】:2014-04-25 15:54:17
【问题描述】:
我需要将对象添加到列表中(现在它们只是 main 中的硬编码对象)
int main ()
{
// create objects of type Point, passing different x and y values to the contructor
Point point1 (7,5); // new point object point1 with value x=2.5, y=5.3
Point point2 (4,8); // second point obejct
Point point3 (8,9); // third point object
Point point4 (10,5);//fourth point object
Point point5 (6,8);//fifth point
Point point6 (4,7);//sixth point
}
使用 push_back 将它们一一添加到列表中
list<Point>pointList ; // stl list that will contain my objects.
pointList.push_back(point1);//adds the obejcts to the list from the back each time {point1}
pointList.push_back(point2);//{point1, point2} < point2 pushed_back
pointList.push_back(point3);//{point, point}
pointList.push_back(point4);
pointList.push_back(point5);
pointList.push_back(point6);
现在我需要做的是一次将对象 3 传递给这个方法 >
static void calculateF (Point p1, Point& p2, Point p3)// point2 needs to be a reference otherwise f is just changed locally.
double F ; //
double xPoint1 = p1.getX(); // x value of object p1
double yPoint1 = p1.getY(); // y value of object p1
double xPoint2 = p2.getX(); // x value of object p2
double yPoint2 = p2.getY(); // y coordinates of object p2
double xPoint3 = p3.getX(); // x coordinates of obejct p3
double yPoint3 = p3.getY(); // y coorinates of obejct p3
//equation for working out f from these six values
//temp variables to store the length of the triangles sides.
double p2p3 = sqrt (((xPoint2 - xPoint3)*(xPoint2 - xPoint3)) + ((yPoint2 - yPoint3)*(yPoint2 - yPoint3))); //length point2 to point1 (PR in assingment notes)
cout << "p1p2 is = " << p2p3 << endl; // print the value of p2p3 (PR)
double p1p2 = sqrt (((xPoint1 - xPoint2)*(xPoint1 - xPoint2)) + ((yPoint1 - yPoint2)*(yPoint1 - yPoint2))); //length point1 to point 2 (LP in assingment notes)
cout << "p1p2 is = " << p1p2 << endl;
double p1p3 = sqrt (((xPoint1 - xPoint3)*(xPoint1 - xPoint3)) + ((yPoint1 - yPoint3)*(yPoint1 - yPoint3)));//length of point1 to point 3 (LR in assigment notes)
cout << "hypotenuse p1p3 is = " << p1p3 << endl;
F = p1p2 + p2p3 - p1p3 ; //equation for f
//cout << "importance factor is " << F << endl ;
p2.setF(F); // setting F to f in class
}
现在我明白我可以一次通过一个 计算(点 1,点 2,点 3); 计算(point2.point3,point4); 等等……
它实际上根本没有使用列表来传递它们,这是我需要做的。在列表中添加点并使用列表将每个点传递给 calculateF 方法。
如果那是一种非常全面的提问方式,我表示歉意。我只是将所有内容都包含在内,以便可以将其放入上下文中。
提前感谢有能力的程序员
【问题讨论】:
标签: c++ list loops visual-c++ iterator