I am a student trying to work from scratch using STL. I am now tasked with implementing iterators. Yes, I know that he invents the wheel, but it will help me better understand how they work and how they can be applied. Hopefully people can indicate if my implementation is too simplified or something is missing.
// ptrdiff_t #include <cstddef> template <class Category, class T, class Distance = ptrdiff_t, class Pointer = T*, class Reference = T&> struct iterator { typedef T value_type; typedef Distance difference_type; typedef Pointer pointer; typedef Reference reference; typedef Category iterator_category; }; template <class Iterator> class iterator_traits { typedef typename Iterator::different_type difference_type; typedef typename Iterator::value_type value_type; typedef typename Iterator::pointer pointer; typedef typename Iterator::reference reference; typedef typename Iterator::iterator_category iterator_category; }; struct random_access_iterator_tag { }; template <class T> class iterator_traits<T*> { typedef ptrdiff_t difference_type; typedef T value_type; typedef T* pointer; typedef T& reference; typedef random_access_iterator_tag iterator_category; }; namespace detail { template <class InputIterator> void foo(InputIterator& i, random_access_iterator_tag) { } } template <class InputIterator> void foo(InputIterator& i) { typename iterator_traits<InputIterator>::iterator_category category; detail::foo(i, category); }
source share