简体   繁体   English

迭代器和反向迭代器

[英]Iterator and reverse Iterator

I am quiet fresh to C++ and programming in general, I am writing an OpenCv application in C++ environment. 我对C ++和程序设计一无所知,我正在C ++环境中编写一个OpenCv应用程序。

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. 好的,所以我在向量中存储了一些矩形中心点,现在我使用反向迭代器对具有矩形中心点的向量进行迭代,并将每第10个中心点存储到新向量中。

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 然后,我再次迭代使用普通迭代器存储第10个矩形中心点的新向量,我想从第2个元素中减去第1个元素,从第4个元素中减去第3个元素,依此类推,减法结果,我想存储到另一个新向量中: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? 第一个问题是,我在运行程序时遇到运行时错误,我相信这是因为在向量Rightarm&Leftarm的开头没有10个元素,并且Iterator运行它并尝试寻找第10个元素时,我无法....那我怎么解决呢?

2nd Problem is to do with this line summery = *RresultIter - *RresultIter++; 第二个问题是关于这一行的问题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... 我知道这是错误的,这是我能想到的最佳尝试,但是我想做的是从第二个元素中减去第一个元素并将其存储在summery元素中...

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. 正如您已经正确注意到的那样,除非Rightarm.size() 10的Rightarm.size()数倍,否则此方法将不起作用。解决此问题的一种方法是跳过开头的元素,以使结尾Rightarm.size()

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

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. 这将复制第一个值而不会产生任何差异(类似于假设“ before-the-first”值为零),因此, erase()行摆脱了该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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