【问题标题】:Navigate through a list passing objects from the list to a global method c++在列表中导航,将列表中的对象传递给全局方法 c++
【发布时间】: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


    【解决方案1】:

    你也可以用向量迭代器做一些更通用的事情,比如;

    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);
        ...
    }
    

    【讨论】:

      【解决方案2】:

      由于列表是作为双向链表实现的,因此您可能更适合使用向量(因为您可以使用向量非常轻松有效地访问各个元素...)。

      您可以使用以下代码示例来执行您想要的操作

      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] );
      }
      

      我希望这会有所帮助...

      【讨论】:

      • 您好 Rich,感谢您的回复。现在我被引导使用列表,因为每次遍历列表时我都需要删除重要性因子最低的点。因此,在我删除了形状中最不重要的点之后,我也需要每次都重建它,然后再次运行它,直到剩余用户指定数量的点(这些点是形状边界的坐标)线 vec.push_back(新点(0,0));似乎也没有工作。
      • 好的,那么 vec.push_back(point(0,0)) 是用来连续添加项目到列表中的吗?然后 for 循环将所有对象传递给然后添加到列表中的计算 f 方法?你能再分解一下吗?对不起我的cmets的noobishness
      • 是的,您将所有想要的点添加到向量中,是的,for 循环然后将向量的元素传递给 calculateF 函数,正如您在初始帖子中详述的那样。它的工作原理如下:calculateF( element1, element2, element3 );calculateF( element2, element3, element4 );,直到它完成......抛出了什么错误?
      • 1>c:\users\max\documents_uni_corsework\year 2 (undergraduate)\c++\cppprojects\assignment\assignment_practicevectors.cpp(126): error C3867: 'std::vector<_ty>: :size': 函数调用缺少参数列表;使用 '&std::vector<_ty>::size' 创建指向成员 1>c:\users\max\documents_uni_corsework\year 2 (undergraduate)\c++\cppprojects\assignment\assignment_practicevectors.cpp(126) 的指针:错误C2296: '-' : 非法,左操作数的类型为 'unsigned int (__thiscall std::vector<_ty>::* )(void) throw() const'
      • 我会发布整个代码,但在 cmets 中没有足够的空间
      猜你喜欢
      • 2018-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      • 2013-10-05
      • 2010-10-15
      • 2015-06-21
      • 1970-01-01
      相关资源
      最近更新 更多