List Iterator syntax C++

Xlist is a list of objects of the type Vertex, each of these vertex objects has an X and a Y value, for the function(double importanceMethod) at the bottom of my code, I need to input the X and Y coordinates of the current vertex object in a list, as well as the X and Y of the previous and next objects in the list.

I clearly have the wrong syntax for filling my ints with the correct values, but I cannot find the correct one in any of the references for C++.
Can anyone shed any light on this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//method for placing values in correct variables. 
  void getListValues(list<Vertex> Xlist)
{
list<Vertex>::iterator iter = Xlist.begin();
int xP=0, yP=0, xL=0, yL=0, xR=0, yR=0;
		while( iter != Xlist.end())
		{
			

			if(iter == Xlist.begin())
			{

				xP = Xlist[iter].getX();
				yP = Xlist[iter].getY();

				xL = Xlist.end().getX();
				yL = Xlist.end().getY();

				xR = Xlist[iter+1].getX();
				yR = Xlist[iter+1].getY();

			}
			
			if(iter != Xlist.begin() && iter != Xlist.end())
			{
	
				xP = Xlist[iter].getX();
				yP = Xlist[iter].getY();

				xL = Xlist[iter-1].getX();
				yL = Xlist[iter-1].getY();

				xR = Xlist[iter+1].getX();
				yR = Xlist[iter+1].getY();

			}

			double val = importanceMethod(xP, yP, xL, yL, xR, yR);
            iter->setF( val ); 
			++iter;
		}


		xP = Xlist[iter].getX();
		yP = Xlist[iter].getY();

		xL = Xlist[iter-1].getX();
		yL = Xlist.[iter-1].getY();

		xR = Xlist.begin().getX();
		yR = Xlist.begin().getY();


		double val = importanceMethod(xP, yP, xL, yL, xR, yR);
        iter->setF( val );
}

//Mathematical method
double importanceMethod(int xP, int yP, int xL, int yL, int xR, int yR)
{
	int PL = pow((xP + xL),2) + pow((yP + yL),2);
	int PR = pow((xP + xR),2) + pow((yP + yR),2);
	int LR = pow((xL + xR),2) + pow((yL + yR),2);

	int F = sqrt(PL) + sqrt(PR) - sqrt(LR);
	return F;
}


Thank you in advance

Jack Perry
1. end() iterator points to the one past the end element. Trying to dereference it will lead to carash in best case
2. List is not random access containe, so it does not have a [] operator.
3. You do not need to calculate all xP, xL and xR every time. Note that on each subsequent iteration xR becomes xP, and xP becomes xL. Just handle beginning of the array and everything will be simple.
4. Thing about iterators like of pumped up pointers and use them that way.
Topic archived. No new replies allowed.