Qt中Java风格的迭代器的原理是什么

friendbkf 2015-06-22 04:54:34


书上说Qt中Java风格的迭代器 指向的是两个元素之间的位置,而不是直接指向元素。
这是一种什么状态?我实在是无法想象。
对于STL风格的迭代器比较熟悉,STL风格的迭代器是直接指向每个元素的。

求解释一下,Java风格的迭代器 指向的是两个元素之间的位置,这是一种什么状态?
能讲一下原理更好哒。
...全文
321 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
friendbkf 2015-06-23
  • 打赏
  • 举报
回复
引用 4 楼 dbzhang800 的回复:
书中说的没什么大问题,如果你用它的话,应该不难理解的,举个例子
    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
看原代码, inline const T &next() { return *i++; } \ inline const T &peekNext() const { return *i; } \ next()返回的是当前指向的元素,之后将迭代器自增1. 表明类java风格的迭代器并不是指向两元素之间。
dbzhang800 2015-06-23
  • 打赏
  • 举报
回复
书中说的没什么大问题,如果你用它的话,应该不难理解的,举个例子
    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
donwmufromdying 2015-06-23
  • 打赏
  • 举报
回复
另外,最新的Qt5.4里边基本上已经不再鼓励使用qSort之类的函数了,而是鼓励使用std::sort等替代。并说明std库的算法是最优算法
donwmufromdying 2015-06-23
  • 打赏
  • 举报
回复
你看的什么书啊,严重误导。
dbzhang800 2015-06-23
  • 打赏
  • 举报
回复
引用 5 楼 friendbkf 的回复:
[quote=引用 4 楼 dbzhang800 的回复:] 书中说的没什么大问题,如果你用它的话,应该不难理解的,举个例子
    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
看原代码, inline const T &next() { return *i++; } \ inline const T &peekNext() const { return *i; } \ next()返回的是当前指向的元素,之后将迭代器自增1. 表明类java风格的迭代器并不是指向两元素之间。[/quote] Qt源码,Qt手册,我的例子 和 你这书中的内容 都是一致的。建议你静下来好好想想 你认为它不指向两元素之间,那么,你怎么取出它当前指向的元素?
friendbkf 2015-06-22
  • 打赏
  • 举报
回复
扒了一下Qt的源代码,看到类java风格的迭代器定义如下:
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的迭代器指向的确实也是当前的元素,并没有什么元素之间的说法。难道是书本误人?

16,240

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧