本页内容

QLinkedListIterator Class

template <typename T> class QLinkedListIterator

QLinkedListIterator 类为 `QLinkedList` 提供了一个 Java 风格的 const 迭代器。更多内容...

头文件: #include <QLinkedListIterator>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core5Compat)
target_link_libraries(mytarget PRIVATE Qt6::Core5Compat)
qmake: QT += core5compat

公共函数

QLinkedListIterator(const QLinkedList<T> &list)
bool findNext(const T &value)
bool findPrevious(const T &value)
bool hasNext() const
bool hasPrevious() const
const T &next()
const T &peekNext() const
const T &peekPrevious() const
const T &previous()
void toBack()
void toFront()
QLinkedListIterator<T> &operator=(const QLinkedList<T> &list)

详细说明

QLinkedList 同时支持Java 风格迭代器和 STL 风格迭代器。Java 风格迭代器比 STL 风格迭代器更高级、更易于使用;但另一方面,其效率略低。

QLinkedListIterator<T> 允许您遍历一个QLinkedList<T>,其中T 是元素类型。如果您希望在遍历列表时对其进行修改,请改用QMutableLinkedListIterator<T>。

QLinkedListIterator 的构造函数接受一个QLinkedList 作为参数。构造完成后,迭代器位于列表的最前端(第一个元素之前)。以下是如何依次遍历所有元素的方法:

QLinkedList<float>list;
...
QLinkedListIterator<float>i(list);
while(i.hasNext())
    qDebug() << i.next();

next() 函数返回列表中的下一个元素,并使迭代器向前移动。与 STL 风格的迭代器不同,Java 风格的迭代器指向元素之间,而不是直接指向元素本身。 第一次调用next() 会将迭代器移动到第一个和第二个元素之间的位置,并返回第一个元素;第二次调用next() 会将迭代器移动到第二个和第三个元素之间的位置,并返回第二个元素;以此类推。

Qt 中 Java 风格迭代器的用法示例

以下是如何按反向顺序遍历元素的方法:

QLinkedListIterator<float>i(list);
i.toBack();
while(i.hasPrevious())
    qDebug() << i.previous();

若要查找某个特定值的所有出现位置,请在循环中使用findNext() 或findPrevious()。

可以在同一个列表上使用多个迭代器。如果在 QLinkedListIterator 处于活动状态时对列表进行了修改,该 QLinkedListIterator 将继续遍历原始列表,而忽略已修改的副本。

另请参阅 QMutableLinkedListIterator 和QLinkedList::const_iterator 。

成员函数文档

QLinkedListIterator::QLinkedListIterator(const QLinkedList<T> &list)

构建一个用于遍历list 的迭代器。该迭代器被设置为位于列表的开头(第一个元素之前)。

另请参阅 operator=()。

bool QLinkedListIterator::findNext(const T &value)

从当前迭代器位置开始向前搜索value 。如果找到了value ,则返回true ;否则返回false 。

调用完成后,如果找到了 `value `,迭代器将定位在匹配项的紧后方;否则,迭代器将定位在容器的末尾。

另请参阅 findPrevious()。

bool QLinkedListIterator::findPrevious(const T &value)

从当前迭代器位置开始向后搜索value 。如果找到了value ,则返回true ;否则返回false。

调用完成后,若找到了 `value `,迭代器将定位在匹配项的前面;否则,迭代器将定位在容器的开头。

另请参阅 findNext()。

bool QLinkedListIterator::hasNext() const

如果迭代器前方至少有一个元素,即迭代器未位于容器的末尾,则返回true ;否则返回false 。

另请参阅 hasPrevious() 和next()。

bool QLinkedListIterator::hasPrevious() const

如果迭代器后方至少有一个元素(即迭代器不在容器的开头),则返回true ;否则返回false 。

另请参见 hasNext() 和previous()。

const T &QLinkedListIterator::next()

返回下一个元素,并将迭代器向前移动一个位置。

若在位于容器末尾的迭代器上调用此函数,将导致未定义的结果。

另请参阅 hasNext()、peekNext() 和previous()。

const T &QLinkedListIterator::peekNext() const

返回下一个元素,同时不移动迭代器。

若在位于容器末尾的迭代器上调用此函数,将导致未定义的结果。

另请参阅 hasNext()、next(),以及peekPrevious()。

const T &QLinkedListIterator::peekPrevious() const

返回前一个元素,同时不移动迭代器。

若在位于容器前端的迭代器上调用此函数,将导致结果未定义。

另请参阅 hasPrevious()、previous() 和peekNext()。

const T &QLinkedListIterator::previous()

返回前一个元素,并将迭代器向后移动一个位置。

若在位于容器前端的迭代器上调用此函数,将导致未定义的结果。

另请参阅 hasPrevious()、peekPrevious() 和next()。

void QLinkedListIterator::toBack()

将迭代器移动到容器的末尾(即最后一个元素之后)。

另请参见 toFront() 和previous()。

void QLinkedListIterator::toFront()

将迭代器移至容器开头(第一个元素之前)。

另请参阅 toBack() 和next()。

QLinkedListIterator<T> &QLinkedListIterator::operator=(const QLinkedList<T> &list)

使迭代器在list 上运行。迭代器被设置为位于列表的开头(第一个元素之前)。

另请参阅 toFront() 和toBack()。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.