QLinkedList::const_iterator Class

class QLinkedList::const_iterator

QLinkedList::const_iterator 类为QLinkedList 提供了 STL 风格的常量迭代器。更多

公共函数

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

详细描述

QLinkedList 同时具有STL 风格迭代器Java 风格迭代器。STL 风格的迭代器更底层,使用起来更麻烦;另一方面,它们的速度稍快,对于已经了解 STL 的开发人员来说,具有熟悉的优势。

QLinkedList<T>::const_iterator 允许你遍历QLinkedList<T>。如果要在遍历过程中修改QLinkedList ,则必须使用QLinkedList::iterator 。一般来说,除非需要通过迭代器修改QLinkedList ,否则在非 ConstQLinkedList 上使用QLinkedList::const_iterator 也是不错的做法。常量迭代器速度稍快,而且可以提高代码的可读性。

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

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

QLinkedList<QString>::const_iterator i;
for (i = list.constBegin(); i != list.constEnd(); ++i)
    cout << *i << Qt::endl;

STL 样式的迭代器可以用作generic algorithms 的参数。例如,下面是在列表中查找项目的方法:

QLinkedList<QString> list;
...
QLinkedList<QString>::const_iterator it = std::find(list.constBegin(),
                                                    list.constEnd(), "Joel");
if (it != list.constEnd())
    cout << "Found Joel" << Qt::endl;

在同一个列表中可以使用多个迭代器。如果向列表中添加项目,现有的迭代器将保持有效。如果从列表中删除项目,指向被删除项目的迭代器将成为悬空迭代器。

警告: 隐式共享容器上的迭代器与 STL-迭代器的工作方式并不完全相同。当容器上的迭代器处于活动状态时,应避免复制该容器。更多信息,请阅读隐式共享迭代器问题

另请参阅 QLinkedList::iteratorQLinkedListIterator

成员函数文档

const_iterator::const_iterator()

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

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

另请参阅 QLinkedList::constBegin() 和QLinkedList::constEnd()。

const_iterator::const_iterator(QLinkedList<T>::iterator other)

构造other 的副本。

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

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

另请参阅 operator==() 。

const T &const_iterator::operator*() const

返回当前项目的引用。

另请参阅 operator->()。

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

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

对于较大的j 值,此操作可能会比较慢。

另请参阅 operator-()。

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

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

QLinkedList<T>::constEnd() 上调用此函数会导致未定义的结果。

另请参阅 operator--() 。

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

这是一个重载函数。

后缀 ++ 运算符 (it++) 将迭代器前进到列表中的下一个项目,并返回一个指向先前当前项目的迭代器。

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

将迭代器向前推进j 项。(如果j 为负数,迭代器会向后退)。

对于较大的j 值,此操作可能会比较慢。

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

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

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

对于较大的j 值,此操作可能会比较慢。

另请参见 operator+()。

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

前缀 - 运算符 (--it) 使前一个项目成为当前项目,并返回一个指向新的当前项目的迭代器。

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

另请参见 operator++()。

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

这是一个重载函数。

后缀 - 运算符 (it--) 使前一个项目成为当前项目,并返回一个指向前一个当前项目的迭代器。

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

使迭代器返回j 项。(如果j 为负数,则迭代器向前移动)。

对于较大的j 值,此操作可能会比较慢。

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

const T *const_iterator::operator->() const

返回指向当前项目的指针。

另请参阅 operator*()。

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

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

另请参阅 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.