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

前端 未结 3 652
误落风尘
误落风尘 2021-01-25 11:46

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

相关标签:
3条回答
  • 2021-01-25 12:13

    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).

    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.

    0 讨论(0)
  • 2021-01-25 12:15

    The reason we have std::advance is that it provides a way to advance an iterator using the most effiecient way supported by that iterator.

    0 讨论(0)
  • 2021-01-25 12:25

    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).

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