Skip an entry of a random iterator

后端 未结 2 1758
耶瑟儿~
耶瑟儿~ 2020-12-22 04:04

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

foo(Rando         


        
相关标签:
2条回答
  • 2020-12-22 04:40

    The answer might be a bit picky, but you could call foo(firsta,i-1) and foo(i+1,lastb) or something similar to have the desired effect.

    0 讨论(0)
  • 2020-12-22 04:52

    The best way to do this would be to use an iterator that knows how to skip that element. A more generalized idea though, is an iterator that simply iterates over two separate ranges under the hood. I don't know of anything in boost that does this, so, here's one I just whipped up: http://coliru.stacked-crooked.com/a/588afa2a353942fc

    Unfortunately, the code to detect which element to skip adds a teeny tiny amount of overhead to each and every iterator increment, so the overhead is technically proportional to lasta-firsta. Realistically, using this wrapper around a vector::iterator or a char* should bring it roughly to the same performance level as std::deque::iterator, so it's not like this should be a major slowdown.

    0 讨论(0)
提交回复
热议问题