0

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 reverse 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, And I want to subtract 1st element from 2nd element 3rd element from 4th element and so on, the subtraction results, I want to store into another new vector :D

It might be slightly confusing to some people; I am confused, myself, that is why below I will add the code I have written.

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 it's because at the begining of 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 it's 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

Nathan Ernst
  • 4,540
  • 25
  • 38
Tomazi
  • 781
  • 9
  • 27
  • 46
  • Are you sure you don't get a compile-time error? `summery = *RresultIter - *RresultIter++` (it's "summary" btw): subtraction doesn't make sense for arrays, and that line is also super-convoluted; at the very least put brackets around the terms. Also you can't do +=10 on an iterator; it could skip past the end of the array and venture into undefined memory. – Dave Mar 29 '13 at 01:09
  • Yes sorry i do get compiler error because of this line when i remove this line I get run time error my best gest would be because of Iterator+=10..........Have you got any idea how could i put all this to work...? regards – Tomazi Mar 29 '13 at 01:12
  • Easiest way is to use your own loop variable, like `for(int i=0;i – Dave Mar 29 '13 at 01:14
  • Ok so just to confirm you are suggesting to use a normal loop instead of the iterators.....? – Tomazi Mar 29 '13 at 01:19
  • Yes. For vectors it compiles to the same thing anyway. As good practice, you should always use `==` and `!=` on iterators (never `<` or `>`). So I suggest just using normal counters instead. Iterators only really become useful with more complicated storage types. – Dave Mar 29 '13 at 01:27
  • @Dave: That particular line is undefined behavior. It is undefined behavior to modify one variable multiple times in a single expression, or to read and modify it unless the read is used only to determine the value to store. From a practical point of view, it is unspecified which side of the substraction will be evaluated first, and that means that the left hand side might be evaluated before or after the increment in the right hand side. Don't do that :) – David Rodríguez - dribeas Mar 29 '13 at 01:30
  • Ok also just forgotten I really do nee to read the values from the end of Rightarm or Leftarm vector and looping will read from the beginning of this vectors....thats why I am trying to use revers Iterator – Tomazi Mar 29 '13 at 01:51
  • You're correct that `*RresultIter - *RresultIter++` is wrong (I believe). It is similar to `int x = i + i++` which is an outright undefined-behavior candidate. Further, I don't believe the deref-op (`operator *()`) make a difference, as with it you're effectively just doing this: `fn(obj) - fn(obj--)`, which I can't see somehow makes it ok. I'm sure to be corrected if this is not the case, however. [This post](http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points) is a wealth of information on sequence points and how they cause undefined behavior. – WhozCraig Mar 29 '13 at 02:35
  • 1
    Please elaborate on the error encountered. "I get an error" is useless as a description, and you're leaving us to speculate. – Nathan Ernst Mar 29 '13 at 03:25
  • Ok so how do i subtract one element of the vector from the next element....? Because what ever I am trying so far gives me run time erros – Tomazi Mar 29 '13 at 03:27
  • @WhozCraig: Even if it was not undefined for that reason, the order of evaluation of the operands of the substraction is unspecified, and the program depends on the order of evaluation which is yet another reason for it to be undefined behavior. – David Rodríguez - dribeas Mar 29 '13 at 04:22

1 Answers1

0

As you've correctly noticed, this won't work unless Rightarm.size() is an exact multiple of 10. One way to work around this is to skip elements at the beginning, to make the end line up.

for(RightMovmentIter = Rightarm.rbegin() + Rightarm.size() % 10;
    RightMovmentIter != Rightarm.rend();
    RightMovmentIter+=10)

As for taking the running difference, there's a standard algorithm for that, std::adjacent_difference.

std::adjacent_difference( RightTracking.begin(), RightTracking.end(),
      std::back_inserter( summery ) );
summery.erase( summery.begin() );

This copies the first value without taking a difference (similar to assuming the "before-the-first" value is zero) so the erase() line gets rid of that.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421