跳过随机迭代器的条目 [英] Skip an entry of a random iterator

查看:285
本文介绍了跳过随机迭代器的条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个函数 foo * firsta 之间的所有元素 / code>和 * lastb

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

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

问题a):有办法跳过索引 firsta< i< lastb by
修改 foo --eg的输入随机迭代器
换句话说,不改变 foo 本身,只是其输入?

question a): is there a way to skip an index firsta<i<lastb by only modifying the inputs to foo --e.g. the random iterators, in other words without changing foo itself, just its input?

不幸的是,我想跳过的索引不在边沿
(它们通常在 firsta lasta )和 foo
是一个复杂的除法和征服算法,不适合
在原始数组的子集上调用迭代器
指向。

--Unfortunately the index I want to skip are not in the edges (they are often deep between firsta and lasta) and foo is a complicated divide&conquer algorithm that's not amenable to being called on subsets of the original array the iterators are pointing to.

问题b):如果进行a)是可能的,这样做的成本是多少?
常数或者它依赖于(lasta-firsta)

question b): if doing a) is possible, what's the cost of doing that? constant or does it depend on (lasta-firsta)?

推荐答案

最好的方法是使用一个知道如何跳过该元素的迭代器。一个更笼统的想法是一个迭代器,它简单地遍历两个独立的范围。我不知道boost中的任何东西,这是,所以,这里是一个我刚刚鞭打: http ://coliru.stacked-crooked.com/a/588afa2a353942fc

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

不幸的是,检测哪个元素跳过的代码会增加少量的开销到每个迭代器增量,所以开销在技术上与 lasta-firsta 成正比。实际上,在 vector :: iterator char * 周围使用这个包装应该大致达到相同的性能水平因为 std :: deque :: iterator ,所以它不是这样应该是一个主要的减速。

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.

这篇关于跳过随机迭代器的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆