本页内容

QLinkedList::iterator Class

class QLinkedList::iterator

QLinkedList::iterator 类为QLinkedList 提供了一个STL风格的非const迭代器。更多内容...

公共函数

iterator()
bool operator!=(const QLinkedList<T>::const_iterator &other) const
bool operator!=(const QLinkedList<T>::iterator &other) const
T &operator*() const
QLinkedList<T>::iterator operator+(int j) const
QLinkedList<T>::iterator &operator++()
QLinkedList<T>::iterator operator++(int)
QLinkedList<T>::iterator &operator+=(int j)
QLinkedList<T>::iterator operator-(int j) const
QLinkedList<T>::iterator &operator--()
QLinkedList<T>::iterator operator--(int)
QLinkedList<T>::iterator &operator-=(int j)
T *operator->() const
bool operator==(const QLinkedList<T>::const_iterator &other) const
bool operator==(const QLinkedList<T>::iterator &other) const

详细说明

QLinkedList 同时具备STL 风格迭代器和 Java 风格迭代器。STL 风格迭代器属于更底层的实现,使用起来较为繁琐;但另一方面,它们的速度稍快,且对于已经了解 STL 的开发人员而言,具有操作熟悉的优势。

QLinkedList<T>::iterator 允许您遍历QLinkedList<T>,并修改与该迭代器关联的列表项。 若要遍历 constQLinkedList ,请改用QLinkedList::const_iterator 。通常,即使针对非 constQLinkedList ,也建议使用QLinkedList::const_iterator ,除非您需要通过迭代器修改QLinkedList 。const 迭代器速度略快,且能提高代码可读性。

默认的QLinkedList::iterator 构造函数会创建一个未初始化的迭代器。在开始遍历之前,必须使用QLinkedList::begin()、QLinkedList::end()或QLinkedList::insert()等函数对其进行初始化。以下是一个典型的循环,用于打印列表中存储的所有项:

QLinkedList<QString> list;
list.append("January");
list.append("February");
...
list.append("December");

QLinkedList<QString>::iterator i;
for (i = list.begin(); i != list.end(); ++i)
    cout << *i << Qt::endl;

STL 风格的迭代器可作为generic algorithms 的参数使用。例如,以下代码演示了如何在列表中查找一个元素:

QLinkedList<QString> list;
...
QLinkedList<QString>::iterator it = std::find(list.begin(),
                                              list.end(), "Joel");
if (it != list.end())
    cout << "Found Joel" << Qt::endl;

下面我们来看几个示例,展示使用QLinkedList::iterator 可以实现而使用QLinkedList::const_iterator 无法实现的功能。以下示例将QLinkedList<int> 中存储的每个值加 2:

QLinkedList<int>::iterator i;
for (i = list.begin(); i != list.end(); ++i)
    *i += 2;

以下是一个示例,用于从 `QLinkedList<QString>` 中移除所有以下划线开头的项:

QLinkedList<QString> list;
...
QLinkedList<QString>::iterator i = list.begin();
while (i != list.end()) {
    if ((*i).startsWith('_'))
        i = list.erase(i);
    else
        ++i;
}

调用QLinkedList::erase() 会将迭代器所指向的项从列表中移除,并返回指向下一个项的迭代器。以下是另一种在迭代过程中移除项的方法:

QLinkedList<QString>::iterator i = list.begin();
while (i != list.end()) {
    QLinkedList<QString>::iterator previous = i;
    ++i;
    if ((*previous).startsWith('_'))
        list.erase(previous);
}

你可能会想写出这样的代码:

// WRONG
while (i != list.end()) {
    if ((*i).startsWith('_'))
        list.erase(i);
    ++i;
}

然而,这种写法在++i 中可能会导致程序崩溃,因为在调用erase() 之后,i 已成为一个悬空迭代器。

同一个列表上可以使用多个迭代器。如果向列表中添加元素,现有的迭代器仍然有效。如果从列表中删除元素,指向已删除元素的迭代器将变成悬空迭代器。

警告: 隐式共享容器上的迭代器其 行为与 STL 迭代器并不完全一致。应避免在迭代器处于活动状态时复制该容器。有关更多信息,请参阅《隐式共享迭代器问题》。

另请参阅 QLinkedList::const_iterator 和QMutableLinkedListIterator 。

成员函数文档

iterator::iterator()

构建一个未初始化的迭代器。

不应在未初始化的迭代器上调用 operator*() 和 operator++() 等函数。使用前请先通过 operator=() 向其赋值。

另请参阅 QLinkedList::begin() 和QLinkedList::end()。

bool iterator::operator!=(const QLinkedList<T>::const_iterator &other) const

bool iterator::operator!=(const QLinkedList<T>::iterator &other) const

如果 `other ` 指向的项与该迭代器指向的项不同,则返回 `true `;否则返回 `false`。

另请参阅 operator==()。

T &iterator::operator*() const

返回对当前项的可修改引用。

您可以通过在赋值语句的左侧使用 *() 运算符来更改项的值,例如:

if (*it == "Hello")
    *it = "Bonjour";

另请参阅 operator->()。

QLinkedList<T>::iterator iterator::operator+(int j) const

返回一个迭代器,该迭代器指向从当前迭代器向前移动j 个位置处的项。(如果j 为负数,则迭代器向后移动。)

当 `j ` 值较大时,此操作可能会较慢。

另请参阅 operator-()。

QLinkedList<T>::iterator &iterator::operator++()

前缀 ++ 运算符(++it )会将迭代器向前移动到列表中的下一个元素,并返回指向新当前元素的迭代器。

在 `QLinkedList::end()` 上调用此函数会导致未定义的结果。

另请参阅 operator--()。

QLinkedList<T>::iterator iterator::operator++(int)

后缀 ++ 运算符(it++ )将迭代器向前移动到列表中的下一个元素,并返回指向之前当前元素的迭代器。

这是一个重载函数。

QLinkedList<T>::iterator &iterator::operator+=(int j)

将迭代器向前移动j 个项目。(如果j 为负数,则迭代器向后移动。)

另请参阅 operator-=() 和operator+()。

QLinkedList<T>::iterator iterator::operator-(int j) const

返回一个迭代器,该迭代器指向从当前迭代器向后移动j 个位置处的项目。(如果j 为负数,则迭代器向前移动。)

当 `j ` 值较大时,此操作可能会较慢。

另请参阅 operator+()。

QLinkedList<T>::iterator &iterator::operator--()

前缀运算符(--it )将前一个元素设为当前元素,并返回指向新当前元素的迭代器。

在 `QLinkedList::begin()` 上调用此函数将导致未定义的结果。

另请参阅 operator++()。

QLinkedList<T>::iterator iterator::operator--(int)

后缀 – 运算符(it-- )将前一个元素设为当前元素,并返回指向先前当前元素的迭代器。

这是一个重载函数。

QLinkedList<T>::iterator &iterator::operator-=(int j)

使迭代器向后移动j 个元素。(如果j 为负数,则迭代器向前移动。)

另请参阅 operator+=() 和operator-()。

T *iterator::operator->() const

返回指向当前项的指针。

另请参阅 operator*()。

bool iterator::operator==(const QLinkedList<T>::const_iterator &other) const

bool iterator::operator==(const QLinkedList<T>::iterator &other) const

如果 `other ` 指向与该迭代器相同的项,则返回 `true `;否则返回 `false`。

另请参阅 operator!=()。

© 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.