16,240
社区成员
发帖
与我相关
我的任务
分享
QList<int> list;
list<<1<<2<<3<<4;
QListIterator<int> it(list); //before the first item
qDebug()<<it.peekNext(); //the first item
it.next(); //between the first and second item, and returns the first item
qDebug()<<it.peekPrevious(); //the first item
qDebug()<<it.peekNext(); //the second item
template <class T> \
class Q##C##Iterator \
{ \
typedef typename Q##C<T>::const_iterator const_iterator; \
Q##C<T> c; \
const_iterator i; \
public: \
inline Q##C##Iterator(const Q##C<T> &container) \
: c(container), i(c.constBegin()) {} \
inline Q##C##Iterator &operator=(const Q##C<T> &container) \
{ c = container; i = c.constBegin(); return *this; } \
inline void toFront() { i = c.constBegin(); } \
inline void toBack() { i = c.constEnd(); } \
inline bool hasNext() const { return i != c.constEnd(); } \
inline const T &next() { return *i++; } \
inline const T &peekNext() const { return *i; } \
inline bool hasPrevious() const { return i != c.constBegin(); } \
inline const T &previous() { return *--i; } \
inline const T &peekPrevious() const { const_iterator p = i; return *--p; } \
inline bool findNext(const T &t) \
{ while (i != c.constEnd()) if (*i++ == t) return true; return false; } \
inline bool findPrevious(const T &t) \
{ while (i != c.constBegin()) if (*(--i) == t) return true; \
return false; } \
};
从源码上来看,类JAVA的迭代器指向的确实也是当前的元素,并没有什么元素之间的说法。难道是书本误人?