在一个列表中导航,该列表将对象从列表传递到全局方法c++
Navigate through a list passing objects from the list to a global method c++
我需要将obejct添加到列表中(目前它们只是主目录中的硬编码对象)
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
}
现在我明白了,我可以像这样一次通过一个calculatef(点1、点2、点3);calculatef(第2点、第3点、第4点);等等…………
它实际上根本没有使用列表来传递它们,这就是我需要做的。在列表中添加点,并使用列表将每个点传递给calculateF方法。
如果这真的是一种全面的提问方式的话,我不赞成。我只是把所有的东西都包括在内,这样它就可以放在上下文中了。
提前感谢有能力的程序员
您还可以使用向量迭代器做一些更通用的事情,比如;
void calculateF (std::vector<Point>::iterator from_a,
std::vector<Point>::iterator to_b )
{
for (std::vector<Point>::iterator i=from_a; i != to_b; ++i) {
// ... do something with i->x and i->y
}
}
main()
{
...
calculateF(vec.begin()+0, vec.begin()+3);
calculateF(vec.begin()+3, vec.begin()+6);
...
}
由于列表是作为双链接列表实现的,因此您可能更适合使用向量(因为使用向量可以非常轻松有效地访问单个元素…)。
您可以使用以下代码示例来执行您想要的
std::vector<Point> vec;
vec.push_back( Point(0,0));
// Push back all other points...
for( int i = 0; i < ( vec.size() - 2 ); i++ )
{
calculateF( vec[i], vec[i+1], vec[i+2] );
}
我希望这能帮助。。。
相关文章:
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 我使用向量来创建类对象列表.初始化向量时如何使用参数调用构造函数?
- 从封装在对象中的函数 C++ 返回时为空的列表
- 如何在不销毁对象的情况下返回对象列表
- 如何使用Q_PROPERTY公开自定义对象列表
- 链接列表在 cpp 中包含不同的对象类
- 如何使用对象制作分数列表并获得平均值
- 什么更好?返回对象指针列表?或返回指向对象列表的指针?
- 如何将成员函数作为参数传递并在派生对象上执行方法列表
- 我可以从列表中获取对象并复制它们,但如何删除我复制的对象?
- 如何获取列表的每个对象并调用getName方法来打印其名称
- 如何在Windows Media Foundation SDK中指定语言列表对象
- 在 DLL 中嵌入 Python:Py_DECREF列表对象时访问冲突读取位置
- 将类的每个实例添加到列表对象的构造函数
- 列表<对象*>迭代器中的分段错误
- 如何使用list::sort()对STL列表对象进行排序,同时将自定义排序函数作为参数传递给list::排序
- 如何防止客户端修改列表对象
- c++列表对象错误
- 在列表对象中使用对象的派生函数
- 编译器错误 使用列表<对象*的迭代器> C++