mardi 7 avril 2020

Implement iterator for std::uniqe_ptr Linked list

This is my code so far, it obviously does not work. I want this iterator to work on both range and increment based for loops. How can I do it?

template<typename T>
class MyList {
  public:
    class Node {
      public:
        Node(const T& data): data(data) {
            this->next = NULL;
        }
        std::unique_ptr<Node> next;
        T data;
    };
    MyList() {
        this->_size = 0;
    }
    int size() const;
    void push_front(const T& data);
    T pop_front();
    T front() const;
    void remove(T data);

    typedef T* iterator;
    typedef const Node* const_iterator;
    iterator begin() {
        return (&_head.get()->data);
    }
    iterator end() {
        return (NULL);
    }

  private:
    int _size;
    std::unique_ptr<MyList<T>::Node> _head;
};

Aucun commentaire:

Enregistrer un commentaire