为什么双向迭代器没有运算符+/-? [英] Why is there no operator +/- for a bidirectional iterator?
问题描述
我自己的推理是没有随机访问,也没有办法知道边界。但是为什么我们有 std :: advance
? 并想一想,为什么没有 std :: deadvance
(因为没有更好的词)?
My own reasoning for it is that there is no random access and there is no way to know the bounds. But then why do we have std::advance
? And come to think of it, why is there no std::deadvance
(for lack of a better word)?
推荐答案
你是对的; 运算符+/-
的要求是它是一个 O(1)
操作,双向迭代器无法满足。 std :: advance
没有这样的速度要求(但会在可用时使用它,例如,用于随机访问迭代器)。
You are right; the requirements for operator+/-
is that it be an O(1)
operation, which cannot be met by bidirectional iterators. std::advance
has no such speed requirement (but will use it when available, e.g., for random access iterators).
请注意,boost具有 boost :: next
和 boost :: previous
迭代器的实现;我不确定他们在标准化方面的地位如何,但是如果它在增强中则很稳定。
Note that boost has boost::next
and boost::prior
implementations for iterators; I'm not sure what their status is on standardization but if it's in boost it's solid.
这篇关于为什么双向迭代器没有运算符+/-?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!