Do you need to implement multiple iterators in an STL-like class?

I am very familiar with STL and how to use it. My question is ...

If I were to implement my own STL container type, how are internal iterators defined? STL classes usually have sequential or arbitrary iterators, constant versions of these, and stream iterators.

Are these iterators fully defined in every STL class, or is there some kind of base class that you inherit to get most of the iterator functionality? Does anyone know a good link on how to implement a class that supports these different iterators?

+6
source share
2 answers

Typically, you need to implement iterator and const_iterator . If reverse iterators are desired, they can be obtained using instances of std::reverse_iterator . Stream iterators will use operator>> and operator<< ; they are generally not suitable for a container (and none of the standard containers provide them).

+7
source

Yes, you need two different iterators that will be fully compatible with stdlib.

You can get most typedefs inherited from std::iterator , but that will not give you any help in the actual implementation.

The Boost.Iterator Facade is trying to simplify the definition of its own iterators, and the tutorial is very useful.

If you try to do this without helpers, you should think about which concept your iterator uses, and then look at the tables in Β§24 of the C ++ standard. They describe all the operations that you need to support and what is the intended semantics.

+4
source

Source: https://habr.com/ru/post/896642/


All Articles