每次传递3个对象时,重复浏览一个列表,调用全局函数

时间:2014-04-26 12:10:15

标签: c++ list loops object stl

所以我在一些obejcts中编写了硬编码,这是类,

class Point // class point will have an x and a y value 
{
public:
// should contain attributes of a single point 
double x ; // x value
double y ; //y value
double f ; //importance factor

//constructor
Point (double xpoint, double ypoint)// can accept two agruments
{
    x = xpoint ;
    y = ypoint ;
}

//default constructor 
Point ()
{
    x = 7;
    y = 8;
}

//set methods
void setX (double pointx)
{
    x = pointx;
}

void setY (double pointy)
{
    y = pointy ;
}

void setF(double inF)
{
    f = inF ;
}

//get methods for retyening values to main
double getX()
{
    return x; // return the X form the setX
}

double getY ()
{
    return y ; // return the y from the setY
}

//return the importance factor
double getF ()
{
    return f ;
}

};

并手动将它们添加到模板列表

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);//point 6


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个对象时,重复遍历列表,调用全局函数calculatef,以便为所有对象分配f值 这是caclulate f方法

//global fucntion that takes three objects as arguments 
static void calculateF (Point& p1, Point& p2, Point& p3)// point2 needs to be a     reference otherwise f is just changed locally. 
{
    double F ; // i now have an f in the class and in the method? do i need both?

    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 
}

到目前为止我能够使用该方法的唯一方法是一次不使用像(point1,point2,point3)这样的列表传递obejcts 3.这是不好的,因为我需要找到点使用最低f值并从列表中删除它。然后再次重新计算f值并重复此过程,直到这些点减少到用户指定的量。因此需要减少点数,因此列表大小也是如此,因此每次将三个对象传递给方法时,我需要导航列表。我希望这个问题能够长篇大论,我只想对整个计划进行概述。

总结一下,我需要浏览每次将三个对象传递给全局方法的列表

非常感谢

编辑:请伙计们,我已经在这几天了,目前为止没有任何帮助是我需要的......或者我需要更多关于它们的信息

代码现在读取

using namespace std ;

class Point // class point will have an x and a y value 
{
public:
// should contain attributes of a single point 
double x ; // x value
double y ; //y value
double f ; //importance factor

//constructor
Point (double xpoint, double ypoint)// can accept two agruments
{
    x = xpoint ;
    y = ypoint ;
}

//default constructor 
Point ()
{
    x = 7;
    y = 8;
}

//set methods
void setX (double pointx)
{
    x = pointx;
}

void setY (double pointy)
{
    y = pointy ;
}

void setF(double inF)
{
    f = inF ;
}

//get methods for retyening values to main
double getX()
{
    return x; // return the X form the setX
}

double getY ()
{
    return y ; // return the y from the setY
}

//return the importance factor
double getF ()
{
    return f ;
}

};

//global fucntion that takes three objects as arguments 
static void calculateF (Point& p1, Point& p2, Point& p3)// point2 needs to be a reference otherwise f is just changed locally. 

{             双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
p2.setF(F);
//cout << "importance factor is " << p2.getF() << endl ;
//p2.setF(F); //  setting F to f in class 

}

int main() {     int userPoints;

cout << "This program will reduce the number of points in shape X to a user defined amount " << endl;
cout << "Enter the number of points you wish this shape to be reduced to:  " << endl ;
cin >> userPoints ;
cout << "this shape will be reduced to: " <<userPoints << " points." << endl;
// create objects of type Point, passing different x and y values to the contructor

Point point1 (1,1); // new point object point1 with value x=2.5, y=5.3
Point point2 (2,2); // second point obejct 
Point point3 (3,3); // third point object
Point point4 (4,4);//fourth point object
Point point5 (5,5);//fifth point
Point point6 (6,6);//point 6



list<Point>pointList ; // stl list that will contain my objects.
//pointList.push_back(point6);//duplicate point
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);
//pointList.push_back(point1);//duplicate point
//add objects to the list


//while lists size is > user points
list<Point>::iterator it1(pointList.begin()), it2(pointList.begin()), it3(pointList.begin()); // 3 iterators all originally pointing to beginning of list
if (it1 != pointList.end()) { ++it2; ++it3; }
if (it2 != pointList.end()) { ++it3; }

int cnt = 1;

do {
     calculateF(*it1, *it2, *it3);

    cout << it1->x << " " << it1->y << " -- ";
    cout << it2->x << " " << it2->y << " -- ";
    cout << it3->x << " " << it3->y << endl;
    cout << "Calc step done " << cnt << "  f is" << it1->f << endl ;
    ++it1;
    ++it2;
    ++it3;

    if ( it1 == pointList.end()) it1=pointList.begin();
    if ( it2 == pointList.end()) it2=pointList.begin();
    if ( it3 == pointList.end()) it3=pointList.begin();
    cnt++;
} while ( it1 != pointList.begin());

/*
while (it3 != pointList.end())
{
   calculateF(*it1, *it2, *it3);
   ++it1;
   ++it2;
   ++it3;
}
*/


//remove smallest f and repeat


/*
list<Point>::iterator pI ;
 //iterator initially points to beginning of list

for (pI = pointList.begin() ; pI!=pointList.end() ; pI++)
{
    cout << *pI ;
}
*/

// calculateF fucntion on point 2
//calculateF(point1, point2, point3);
//calculateF(point2, point3, point4);
//calculateF(point3,point4, point5);
//calculateF(point4,point5,point6);
//point2.getF();
//cout << "point 2 has f value of: " << point2.getF() << endl ;

system ("PAUSE");
return 0 ;

}

1 个答案:

答案 0 :(得分:1)

如果使用向量而不是列表,则可以使用+运算符在列表上使用迭代器,这样您就可以简单地写出:

vector<Point> pointList ; 
pointList.push_back(point1);
pointList.push_back(point2);
pointList.push_back(point3);
pointList.push_back(point4);
pointList.push_back(point5);
pointList.push_back(point6);

if ( pointList.size() >= 3)
{
    vector<Point>::iterator it = pointList.begin();
    for ( it = pointList.begin(); (it+2) != pointList.end(); it++) 
    {   
        calculateF( *it, *(it+1), *(it+2));
    }
} 

//来自Nikos的代码回答评论,因为OP想知道它是如何工作的: - )

// create 3 iterators which points to the first element of the list
list<Point>::iterator it1(pointList.begin());
list<Point>::iterator it2(pointList.begin());
list<Point>::iterator it3(pointList.begin());

// now we have the 3 iterators, the it2 should point on 2. element of list 
// and the it3 should point to the 3. list element.
// An iterator simply points to the next element if we do '++' operation on
// the iterator. Here the syntax and semantic is the same as for "normal" pointers,
// but it works also for list iterators. The ++operator for a list iterator 
// itself knows how to get to the next list element. The iterator ++operator knows
// that there is a link in the list element which is pointing to the next list 
// element. So simple so easy :-)

if (it1 != pointList.end()) { ++it2; ++it3; }
// now it2 points to second element
// and it3 points also to second element

if (it2 != pointList.end()) { ++it3; }
// and now it3 points to the 3. element. Fine!

// now we start a loop, until the it3 points not to the end() of the list.
// any container define end() which is exact the element BEHIND the last valid element
// inside a list. If a list contains 10 elements, end() points to the 11.! which
// is not there and must not be accessed through the iterator!
while (it3 != pointList.end())
{   
    // now we call your function with p1,p2,p3 in the first step
    calculateF(*it1, *it2, *it3); 

    // after that, we set each iterator to the next element
    // it2 -> 2
    // it3 -> 3
    // it4 -> 4
    ++it1;
    ++it2;
    ++it3;

    and run the loop again, until it3 points to end()
} 

我认为你应该阅读c ++中的迭代器。如果要删除数组中某处的元素,使用本机数组将无济于事。

EDIT3:进行循环循环:

int cnt = 1;

do {
    calculateF(*it1, *it2, *it3);

    cout << it1->x << " " << it1->y << " -- ";
    cout << it2->x << " " << it2->y << " -- ";
    cout << it3->x << " " << it3->y << endl;
    cout << "Calc step done " << cnt << "  f is" << it1->f << endl ;
    ++it1;
    ++it2;
    ++it3;

    if ( it1 == pointList.end()) it1=pointList.begin();
    if ( it2 == pointList.end()) it2=pointList.begin();
    if ( it3 == pointList.end()) it3=pointList.begin();
    cnt++;
} while ( it1 != pointList.begin());

将前2个元素添加到结尾是无效的。在删除和计算这些对象中的f时,这会导致非常复杂的结果。对于设计不良的算法来说,只有两倍的东西永远不是一个好主意: - )

但循环现在应该完成这项工作。

再次编辑: 我删除了你的输出,将所有的点值都改为1)1,1 2)2,2等等,输出就是:

1 1 -- 2 2 -- 3 3
Calc step done 1  f is0
2 2 -- 3 3 -- 4 4
Calc step done 2  f is0
3 3 -- 4 4 -- 5 5
Calc step done 3  f is0
4 4 -- 5 5 -- 6 6
Calc step done 4  f is0
5 5 -- 6 6 -- 1 1
Calc step done 5  f is0
6 6 -- 1 1 -- 2 2
Calc step done 6  f is2.82843

我不知道你在计算什么,但循环似乎有效: - )