QLinkedList::const_iterator Class
class QLinkedList::const_iteratorQLinkedList::const_iterator 类为QLinkedList 提供了STL风格的const迭代器。更多内容...
公共函数
| 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 。 通常,即使对于非 const 的QLinkedList ,也建议使用QLinkedList::const_iterator ,除非您需要通过迭代器修改QLinkedList 。const 迭代器速度稍快,且能提高代码可读性。
默认的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::iterator 和QLinkedListIterator 。
成员函数文档
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!=()。
© 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.