Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping an iterator before the end

Tags:

c++

I have an iterator p and a vertex curvePoints:

for (p = curvePoints.begin(); p != curvePoints.end(); p++) {
  p->x = (1 - u) * p->x + u * (p+1)->x;
  p->y = (1 - u) * p->y + u * (p+1)->y;
}

Right now the loops use the value of the next indexed object; how can I guarantee that that next value exists. In other words, how can I make the loop condition something like (p+1) != curvePoints.end() or p != curvePoints.end() - 1.

like image 265
bhan Avatar asked Feb 24 '26 03:02

bhan


1 Answers

You can use std::distance(p, curvePoints.end()) > 1 as you condition.

http://www.cplusplus.com/reference/std/iterator/distance/

like image 134
tomahh Avatar answered Feb 25 '26 17:02

tomahh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!