const-iterator

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

 ̄綄美尐妖づ 提交于 2019-11-26 05:48:19
问题 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 any. I know about the need for const overloads of [] and * operators. What are the requirements for an iterator to be \"STL-style\" and what are some other pitfalls to avoid (if any)? Additional context: This is for a library and I don\'t want to introduce any dependency on it unless I really need to. I write my own

How to correctly implement custom iterators and const_iterators?

﹥>﹥吖頭↗ 提交于 2019-11-26 01:04:30
问题 I have a custom container class for which I\'d like to write the iterator and const_iterator classes. I never did this before and I failed to find an appropriate how-to. What are the guidelines regarding iterator creation, and what should I be aware of ? I\'d also like to avoid code duplication (I feel that const_iterator and iterator share many things; should one subclass the other ?). Foot note: I\'m pretty sure Boost has something to ease this but I can\'t use it here, for many stupid