QLinkedList::iterator Class

class QLinkedList::iterator

QLinkedList::iterator 类为QLinkedList 提供了 STL 风格的非常数迭代器。更多

公共函数

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 代替。一般来说,除非需要通过迭代器修改QLinkedList ,否则在非常量QLinkedList 上使用QLinkedList::const_iterator 也是不错的做法。常量迭代器速度稍快,而且可以提高代码的可读性。

默认的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_iteratorQMutableLinkedListIterator

成员函数文档

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

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

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

另请参阅 operator!=() 。

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

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

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

另请参阅 operator==() 。

iterator::iterator()

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

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

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

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*()。

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