简体   繁体   中英

Iterator and reverse Iterator

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM