【问题标题】:Navigate through a list repeatedly calling a global function each time passing 3 objects在列表中导航,每次传递 3 个对象时重复调用全局函数
【发布时间】:2014-04-26 12:10:14
【问题描述】:

所以我在一些对象中硬编码,这是类,

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 值 这里是计算 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 
}

到目前为止,我能够使用该方法的唯一方法是一次传递对象 3,而不使用 (point1,point2,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. 

{ 双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 ;

}

【问题讨论】:

  • 你在问什么?您是要我们为您制定算法吗?
  • 不,我刚刚遇到了障碍,对列表和迭代器缺乏经验。我不知道每次删除最低点时如何使用迭代器遍历列表。类似于 while list.size>userInput,计算所有点的 F,比较 F 值以找到最低点,删除该点并重复。我想我被迭代器弄糊涂了。

标签: c++ list loops object stl


【解决方案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

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

【讨论】:

  • 您好克劳斯,感谢您的回复。使用向量可以吗?因为每次我找到并删除具有最低 f 值的点时,我都需要重新计算每个点的 f。所以在找到所有 F 值的最后,我需要找到 F 值最低的点。所有这一切都将在 list.size 是 > 用户输入时完成
  • 如果经常删除元素,列表会更好。相反,列表需要更多空间来链接到下一个对象。所以只需使用 Nikos 的代码。它将满足您的需求:-)
  • well 元素将被频繁删除,因为例如我可能有 500 分减少到 10 分。好的,我会尝试从 Niko 那里获取更多信息,非常感谢您的宝贵时间
  • 朋友。我不明白 Nikos 的回答。你能帮忙吗?
  • 我可以放弃所有的 stl 并使用一个数组吗?因为我显然没有得到这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-12
  • 1970-01-01
  • 2012-10-15
  • 2020-07-16
  • 1970-01-01
相关资源
最近更新 更多