0

EDIT: This question was closed even though none of them talked about reverse iterators, so my question still stands


If you want to implement iterators correctly inside a custom container, in such way that supports STL algorithms and range-based for loops you, must use an API and define some functions and typedefs.

What are the requirements of this API for iterators? and reverse iterators?

I know that for iterator (not reverse_iterator) you need your custom container to have the following and I don't know if there's anything else or this really is what you need: (the <> represent a placeholder, and not a template for exmaple)

Inside the Container Class

typedef <IteratorClass> iterator;
typedef <ConstIteratorClass> const_iterator;

iterator begin ();
const_iterator begin () const;
const_iterator cbegin () const;

iterator end ();
const_iterator end () const;
const_iterator cend () const;

Inside the Iterator Class (and you need a const iterator class as well)

typedef <ValueType> value_type;
typedef <ReferenceType> reference;
typedef <PointerType> pointer;
typedef <DiffType> difference_type;
typedef <IteratorCategory> iterator_category;

Does reverse Iterator require something similar?

Ariel Yael
  • 361
  • 2
  • 10
  • Does this explain what you are looking for? https://en.cppreference.com/w/cpp/iterator/reverse_iterator – Pepijn Kramer Sep 18 '21 at 06:04
  • @PKramer from what I understand this only talks about iterator traits, which are the 5 tags I put inside the Iterator class. This doesn't answer my question though, since I want to know the complete API (both for the container class and the iterator class, and for reverse iterators and the functions+typedefs you need inside *the container* to support the reverse iterator) – Ariel Yael Sep 18 '21 at 06:04
  • @PKramer and I've edited mine :) – Ariel Yael Sep 18 '21 at 06:05
  • @PKramer for example I remember that inside the container (for reverse iterator) you need to implement `rbegin` and `rend`. But I couldn't find any place with a complete API description – Ariel Yael Sep 18 '21 at 06:07
  • Reverse iterator is an adapter, with the same "interface" as a normal iterator. I say "interface" because in STL no inheritance is used. There is no default API, just the set of traits an iterator has to support. – Pepijn Kramer Sep 18 '21 at 06:07
  • Lol maybe the real answer is best left to someone more experienced in iterators then I am. I've only done it a few times and I remember I just made classes conforming to the traits (which I think is the API definition of an iterator) – Pepijn Kramer Sep 18 '21 at 06:09
  • You need to 1) add type aliases into the iterator class or provide a specialization of [`std::iterator_traits`](https://en.cppreference.com/w/cpp/iterator/iterator_traits), 2) implement operations for the iterator category of your iterator that satisfy all the [requirements](https://en.cppreference.com/w/cpp/named_req#Iterator). That's all. You can implement a reverse iterator manually in exactly the same way, if there is a reason to. – Evg Sep 18 '21 at 06:14
  • @Evg what about reverse iterator functions( such as `rend`)? – Ariel Yael Sep 18 '21 at 13:27
  • @ArielYael You can either use `std::reverse_iterator` adaptor or provide your own implementation. – Evg Sep 18 '21 at 18:05

0 Answers0