How to implement an STL-style iterator and avoid common pitfalls?

后端 未结 8 856
刺人心
刺人心 2020-11-22 16:45

I made a collection for which I want to provide an STL-style, random-access iterator. I was searching around for an example implementation of an iterator but I didn\'t find

8条回答
  •  -上瘾入骨i
    2020-11-22 17:34

    First of all you can look here for a list of the various operations the individual iterator types need to support.

    Next, when you have made your iterator class you need to either specialize std::iterator_traits for it and provide some necessary typedefs (like iterator_category or value_type) or alternatively derive it from std::iterator, which defines the needed typedefs for you and can therefore be used with the default std::iterator_traits.

    disclaimer: I know some people don't like cplusplus.com that much, but they provide some really useful information on this.

提交回复
热议问题