Original Post
Are there any patterns for when you need to iterate over a range but also access the next element?
For example something like this
[source lang='cpp']
std::vector v;
for(int i=0; i {
int& a = v.at(i);
int& b = v.at((i+1) % v.size());
foo(a,b);
}
[/source]
There's a lot of repetition and boilerplate code here but I can't think of any good ways to eliminate it. Boost foreach is good for regular iteration but I don't think it can do stuff like this.
For example something like this
[source lang='cpp']
std::vector
for(int i=0; i
int& a = v.at(i);
int& b = v.at((i+1) % v.size());
foo(a,b);
}
[/source]
There's a lot of repetition and boilerplate code here but I can't think of any good ways to eliminate it. Boost foreach is good for regular iteration but I don't think it can do stuff like this.