Skip random iterator entry

Suppose we have a function foo that does something for all elements between *firsta and *lastb :

 foo(RandomAccessIterator1 firsta,RandomAccessIterator1 lasta){ for (RandomAccessIterator1 it=firsta;it!=lasta+1;it++){ //here stuff happens... } } 

question a): is there a way to skip the index firsta<i<lastb only changing the inputs to foo --eg random iterators, in other words, without changing the foo itself, only its input?

- Unfortunately, the index that I want to skip is not in the edges (they often are deep between firsta and lasta ) and foo is a complex division and rest algorithm that cannot be called up to call subsets of the original array, iterators point to.

Question b): if you do a) is it possible what is this cost? constant or dependent on (lasta-firsta) ?

+1
source share
2 answers

The best way to do this is to use an iterator that knows how to skip this element. However, a more general idea is an iterator that simply iterates over two separate ranges under the hood. I donโ€™t know anything in boost that does this, so here I just whipped: http://coliru.stacked-crooked.com/a/588afa2a353942fc

Unfortunately, the code to detect which element is skipped adds a tiny amount of overhead for each iterator as it grows, so the overhead is technically proportional to lasta-firsta . Actually, using this wrapper around vector::iterator or char* , it should approach the same level of performance as std::deque::iterator , so this should not be a serious slowdown.

+3
source

The answer may be a little picky, but you can name foo(firsta,i-1) and foo(i+1,lastb) or something similar to the desired effect.

+1
source

Source: https://habr.com/ru/post/969715/


All Articles