Skip an entry of a random iterator

后端 未结 2 1771
耶瑟儿~
耶瑟儿~ 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: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.

提交回复
热议问题