简体   繁体   中英

Why is there no operator +/- for a bidirectional iterator?

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 ? [EDIT] And come to think of it, why is there no std::deadvance (for lack of a better word)?

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, eg, for random access iterators).

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.

The rational for std::advance is that it should be obvious that you really intended to use it, even if it is not O(1).

You don't need a std::deadvance as you can use std::advance with a negative distance (for bidirectional iterators).

我们之所以拥有std::advance是因为它提供了一种使用迭代器支持的最有效方式来推进迭代器的方法

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM