Iterators

Hello People...!

I am quiet fresh to C++ and programming in general, I am writing an OpenCv application in C++ environment.

WHAT I AM TRYING TO ACHIEVE:

OK so I got some Rectangles center points stored in a vector, Now i am using a revers Iterator to iterate over the vector with rectangle center points and store every 10th center point into new vector.

I then again iterate over that new vector that stores every 10th rectangle center point with normal iterator, An i want to subtract 1st element from 2nd element 3rd element form 4th element and so on, The subtraction results I want to store into another new vector :D

It might b slightly confusing to some people am confused my self that is why below I will add the code I have written.



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
	vector<Point> Rightarm;
	vector<Point> Leftarm;

vector<Point>::reverse_iterator RightMovmentIter;
	vector<Point>::reverse_iterator LeftarmMovmentIter;

	vector<Point> RightTracking;
	vector<Point> LeftTracking;


		for(RightMovmentIter = Rightarm.rbegin(); RightMovmentIter != Rightarm.rend(); RightMovmentIter+=10)
		{
			RightTracking.push_back(*RightMovmentIter);
		}
	

	
		for(LeftarmMovmentIter = Leftarm.rbegin(); LeftarmMovmentIter != Leftarm.rend(); LeftarmMovmentIter+=10)
		{
			LeftTracking.push_back(*LeftarmMovmentIter);
		}
	


	vector<Point>::iterator RresultIter;
	vector<Point>::iterator Leftresult_Iter;

	vector<Point> summery;


	for(RresultIter = RightTracking.begin(); RresultIter != RightTracking.end(); RresultIter++)
	{
		summery = *RresultIter - *RresultIter++;

	}



PROBLEMS:

1st Problem is that when i run the program I get run time error i belief its because at the beguining the vector Rightarm & Leftarm do not have 10 elements and when the Iterator runs through it and is trying to look for the 10th element i cant....HOW do I work this out then

2nd Problem is to do with this line summery = *RresultIter - *RresultIter++; I know its wrong and This is the best attempt i could of think of, But what i want to do is to subtract 1st element from 2nd element and store it in summery element......

Hopefully This describes my problem well enough for the readers
Regards
I'm not sure about your first problem, it sounds like you are not too sure either, did you confirm the problem line by stepping through it?

The 2nd problem, you are trying to assign a Point value to a vector<Point> object, you should be using push_back I assume.

 
summery = *RresultIter - *RresultIter++;


 
summery.push_back( *RresultIter - *RresultIter++ );
What kind of type is "Point"?
The type of Point is irrelevant.

For the first issue, I think the best thing to do is use the < operator instead of != operator in your condition for statement. This is because if you can't guarantee that the reference vector has a multiple of 10 values, then it's unlikely that you'll always get to a point where RightMovmentIter==Rightarm.rend();

For the second issue;
bradw is partially right. You need to use push_back, but that isn't the only problem. In that statement, you are saying the summary is equal to the Rresult - Rresult which is zero. Then you are incrementing Rresult.

I'm not sure exactly what you are trying to do, but I suggest one of the following two approaches:
summary.push_back(*RresultIter - *(RresultIter+1) );
or
summary.push_back(*RresultIter - *(++RresultIter) );

The first will simply access the next element, the second will increment the iterator as well. Since you increment at the end of the for loop, you are incrementing twice per loop. That means if I have a vector of values like this:
1 3 6 10
the output vector from the first method would be
2 3 4
. The output vector from the second method would be
2 4
(The 3 is skipped). It's up to you which you want.

Regardless of what method you choose, you also have to change your for loop to ensure that you don't go beyond the bounds of the vector when you do this:
for(RresultIter = RightTracking.begin(); RresultIter != RightTracking.end()-1; RresultIter++)
Since you know that you are going to access one further element, you don't want to go all of the way to the end of the vector and subtract a non existent element from the last one.
Last edited on
Topic archived. No new replies allowed.