2014-04-26 56 views
-1

所以我硬编码在一些obejcts,这里是类重复调用一个全局函数,导航通过列表每次路过3个对象

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 
} 

我已经能够通过一次传递obejcts 3不使用列表像(点1,点2,POINT3)等使用的方法,到目前为止是唯一的办法这是没有好,因为我需要用以找到点最低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. 

{Fdouble 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主() { 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 ; 

}

+0

你在问什么?你是在问我们为你制定一个算法吗? –

+0

不,我只是碰到一个块,并且对列表和迭代器没有经验。我不知道如何使用迭代器遍历列表,每次删除最低点。类似于list.size> userInput,为所有点计算F,比较F值找到最低值,删除此点并重复。我想我会被迭代器弄糊涂了。 – punity

回答

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

//代码从尼科斯被盗回答评论它,因为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 

我不知道是什么你正在计算,但循环似乎工作:-)

+0

嗨克劳斯,谢谢你的回应。将使用向量可以吗?因为每次我找到并删除f值最低的点时,都需要重新计算每个点的f值。因此,在查找所有F值后我需要找到具有最低F值的点。所有这些都会在list.size>用户输入时有所不同 – punity

+0

如果元素经常被移除,列表会更好。相反,列表需要更多的空间来链接到下一个对象。所以简单地使用Nikos的代码。它会适合你的需求:-) – Klaus

+0

井元素将被频繁地删除,因为我可能有500点,例如减少到10。好吧,我会尝试从Niko获得更多信息,非常感谢您的时间 – punity