template<class I, class T>
class elm::PreIterator< I, T >
This class is an helper in the writing of iterators. It avoids to redefine all iterator operators each time an operator is defined. The user of this class has just to define four methods:
- bool ended() const – that returns true when the iteration ends,
- bool equals(const Iterator& i) const – to test if two iterators are equal,
- void next() – to move to the next item,
- T item() const – to get the current item (if any).
Using these 4 functions, PreIterator is able to support the operators (), !, ++ (prefix), ++ (suffix), *, ->, == and !=.
For example, the iterator on an array can be written as:
template <class T>
class ArrayIter: public PreIterator<ArrayIter, T> {
public:
inline ArrayIter(
int number, T *
array)
: n(number), i(0), a(
array) { }
inline bool ended() const { return i >= n; }
inline bool equals(
const ArrayIter& it)
{ return a == it.a && n == it.n && i == it.i; }
inline void next() { i++; }
inline T item() const { return a[i]; }
private:
int n, i;
T *a;
};
- Parameters
-
I | Type of the defined iterator. |
T | Type of the items. |