Search code examples
c++listiteratoroperatorsdereference

Implementing a list iterator and operators: -- ++ *


Following on from this question: std::list implementation & pointer arithemetic.

I want to implement a list iterator that is interchangeable with other common containers types and their respective iterators, so I want to use operators such as: --, ++, * and be able to declare iterators as normal, so: list::iterator iter = list.begin();

The --, ++ operators now work as they should, but I ran up against the problem of de-referencing the iterator, as structs can't return a value: T iterator::operator*()

template <class T> 
struct element {

  element<T> *prev = NULL;
  element<T> *next = NULL;
  T data;       
};


template <typename T>
class list {

public:

    list::list();

    element<T>* current;

    struct iterator{

        element<T>* iterator::operator++(){ 

           this = *this->next; ..whatever it works          
        }
        element<T>* iterator::operator--()
        T iterator::operator*()
   }    
};

Solution

  • the iterators could look something like this:
    (declared inside the list template)

        struct iterator;
        struct const_iterator : public std::iterator<std::bidirectional_iterator_tag, const T>
        {
            const_iterator() = default;
            T operator*() { return itm->data; }
            const T* operator->() { return &(itm->data); }
            const_iterator operator++() { itm = itm->next; return *this; }
            const_iterator operator--() { itm = itm->prev; return *this; }
            const_iterator operator++(int) { const_iterator ret=*this; itm = itm->next; return ret; }
            const_iterator operator--(int) { const_iterator ret=*this; itm = itm->prev; return ret; }
            bool operator==(const_iterator oth) const { return itm==oth.itm; }
            bool operator!=(const_iterator oth) const { return itm!=oth.itm; }
        private:
            element<T>* itm = nullptr;
            const_iterator(element<T>* i) : itm(i) {}
        friend
            class list;
        friend
            struct iterator;
        };
        struct iterator : public std::iterator<std::bidirectional_iterator_tag, T>
        {
            iterator() = default;
            T& operator*() { return itm->data; }
            T* operator->() { return &(itm->data); }
            iterator operator++() { itm = itm->next; return *this; }
            iterator operator--() { itm = itm->prev; return *this; }
            iterator operator++(int) { iterator ret=*this; itm = itm->next; return ret; }
            iterator operator--(int) { iterator ret=*this; itm = itm->prev; return ret; }
            bool operator==(iterator oth) const { return itm==oth.itm; }
            bool operator!=(iterator oth) const { return itm!=oth.itm; }
            operator const_iterator() { return {itm}; }
        private:
            element<T>* itm = nullptr;
            iterator(element<T>* i) : itm(i) {}
        friend
            class list;
        };
    
        typedef std::reverse_iterator<iterator> reverse_iterator;
        typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
    

    this needs

    #include <iterator>
    

    to compile