Run Time error while iterating over a vector

Hello people

I am Iterating over a vector that stores Point data type elements [x,y], And what i want to achieve at every iteration is to add first 4 point x elements and first 4 points y elements....The during the next iteration i want to do the same with the next 4 x & y......and so on

1st iteration 1.x + 2.x + 3.x + 4.x
1.y + 2.y + 3.y + 4.y

2nd iteration 5.x + 6.x + 7.x + 8.x
5.y + 6.y + 7.y + 8.y

and so on.....................Hope my problem is clear

Sample code:

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
	double Pta; 
	double Ptb;
	Point  vect;
	float R1, R2;
	float resultant;
	vector<Point> vect_add;
	for(vector<Point>::iterator iter_a = Left_Arm_xy.begin()+1; iter_a != Left_Arm_xy.end(); ++iter_a)
	{
		
		if(center.y <= 240)
		{
			vect.x = iter_a->x - (iter_a -1)->x;
			vect.y = iter_a->y - (iter_a -1)->y;

			vect_add.push_back(Point(vect.x,vect.y));

			
			for(vector<Point>::iterator iter_v = vect_add.begin(); iter_v != vect_add.end(); ++iter_v)
			{
				
				if(vect_add.size() >4)
				{
					R1 = iter_v->x + (iter_v +1)->x + (iter_v +2)->x + (iter_v +3)->x;
					R2 = iter_v->y + (iter_v +1)->y + (iter_v +2)->y + (iter_v +3)->y;
				
					resultant = sqrt(pow(R1,2) + pow(R2,2));
						
					

					cout<<"Resultant: "" "<<resultant<<endl;
				}
			}
			
		}


When i comet out the part where I am trying to add all the x's and y's the program works fine just like it did before.....Therefore the issue must me there..I guess the syntax is not right but am not the best programmer and couldn't this of whats wrong as there is no compiler warnings....the program just crashes at run time

Can anyone possibly see what I am doing wrong with this code
regards
Last edited on
Why are you clearing the vector in your loop? If you add or remove elements of a vector you invalidate any iterators.

Yes that right my bad sorry....forgotten to remove this statment before posting this query......Why i tried it in the first place is that i literary tried everything that i know :D if that makes sens....anyway i alter this post so that it no more cases confusing. regards
Topic archived. No new replies allowed.