Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you have to implement multiple iterators in a STL-like class?

Tags:

c++

stl

I'm quite familiar with the STL and how to use it. My question is...

If I were to implement my own STL container type, how are the internal iterators defined? STL classes tend to have sequential or random-access iterators, const_ versions of these, and stream iterators.

Are these iterators all fully-defined in every STL class, or is there some sort of base class that you inherit from to gain most of the iterator functionality? Does anyone know a good reference for how to implement a class that supports these different kinds of iterators?

like image 895
John Humphreys Avatar asked Sep 05 '11 13:09

John Humphreys


1 Answers

Generally, you only have to implement iterator and const_iterator. If reverse iterators are desired, they can be obtained using instantiations of std::reverse_iterator. The stream iterators will use operator>> and operator<<; typically, they aren't appropriate for a container (and none of the standard containers provides them).

like image 169
James Kanze Avatar answered Oct 08 '22 00:10

James Kanze