Code for a basic random access iterator based on pointers?

后端 未结 3 1121
醉梦人生
醉梦人生 2020-12-28 20:35

I\'ve never implemented STL-like iterators and I try to understand how to implement a very basic thing based on pointers. Once I will have this class I will be able to modif

3条回答
  •  独厮守ぢ
    2020-12-28 20:56

    In general your approach is right. The postfix increment/decrement operator should return by value, not by reference. I also have doubts about:

    Iterator(Type* rhs) : _ptr(rhs) {;}
    

    This tells everyone that this iterator class is implemented around pointers. I would try making this method only callable by the container. Same for assignment to a pointer. Adding two iterators makes no sense to me (I would leave "iterator+int"). Substracting two iterators pointing to the same container might make some sense.

提交回复
热议问题