Reference Guide: List Iterators

By virtue of polymorphism, all List implementations use a single ListIterator that hides complexity of actual solution used (eg: ArrayList). To make this possible, each class implementing List needs to come with a ListIterator implementation adapted to the data structure they are using and keep it hidden to end users so they only work with abstractions.

Usage example:

ArrayList<long> list; list.addToTail(1); list.addToTail(2); // This uses ListIterator<long>* for List<long>*, instead of ArrayList<long> for(auto it = list.begin(); *it!=*(list.end()); ++(*it)) { std::cout << (*(*it)) << std::endl; }

Whereas STL equivalent is:

std::vector<long> list; list.push_back(1); list.push_back(2); // This uses iterator for std::vector<long> directly for(auto it = list.begin(); it!=list.end(); ++it) { std::cout << (*it) << std::endl; }

As one can see above, because API iterators are polymorphic, they require an extra pointer compared to those of STL. Unlike STL, currently only forward non-const iterators are supported, but these will be added in the near future!

ListIterator

Forward iterator prototype for List, returned by latter's begin and end methods

Signature

template <typename T> class ListIterator

Methods

Method Signature Description
~ListIterator virtual ~ListIterator () Virtual destructor used when iterator is deallocated
operator* virtual const T& operator* () = 0 Dereferencing operator required when accessing current element
operator++ virtual void operator++ () = 0 Advance operator required when iterating
operator!= bool operator!= (const ListIterator<T>& it) Difference operator required when iterating

Share