QLinkedList Class
template <typename T> class QLinkedListQLinkedList 类是一个提供链表功能的模板类。更多内容...
| 头文件: | #include <QLinkedList> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core5Compat) target_link_libraries(mytarget PRIVATE Qt6::Core5Compat) |
| qmake: | QT += core5compat |
- 所有成员的列表,包括继承的成员
- QLinkedList 属于“隐式共享类”的一部分。
注意:该类中的所有函数均为可重入函数。
公共类型
| class | const_iterator |
| class | iterator |
| ConstIterator | |
| Iterator | |
| const_pointer | |
| const_reference | |
| const_reverse_iterator | |
| difference_type | |
| pointer | |
| reference | |
| reverse_iterator | |
| size_type | |
| value_type |
公共函数
| QLinkedList() | |
| QLinkedList(std::initializer_list<T> list) | |
| QLinkedList(InputIterator first, InputIterator last) | |
| QLinkedList(const QLinkedList<T> &other) | |
| QLinkedList(QLinkedList<T> &&other) | |
| ~QLinkedList() | |
| void | append(const T &value) |
| T & | back() |
| const T & | back() const |
| QLinkedList<T>::iterator | begin() |
| QLinkedList<T>::const_iterator | begin() const |
| QLinkedList<T>::const_iterator | cbegin() const |
| QLinkedList<T>::const_iterator | cend() const |
| void | clear() |
| QLinkedList<T>::const_iterator | constBegin() const |
| QLinkedList<T>::const_iterator | constEnd() const |
| bool | contains(const T &value) const |
| int | count() const |
| int | count(const T &value) const |
| QLinkedList<T>::const_reverse_iterator | crbegin() const |
| QLinkedList<T>::const_reverse_iterator | crend() const |
| bool | empty() const |
| QLinkedList<T>::iterator | end() |
| QLinkedList<T>::const_iterator | end() const |
| bool | endsWith(const T &value) const |
| QLinkedList<T>::iterator | erase(QLinkedList<T>::iterator pos) |
| QLinkedList<T>::iterator | erase(QLinkedList<T>::iterator begin, QLinkedList<T>::iterator end) |
| T & | first() |
| const T & | first() const |
| T & | front() |
| const T & | front() const |
| QLinkedList<T>::iterator | insert(QLinkedList<T>::iterator before, const T &value) |
| bool | isEmpty() const |
| T & | last() |
| const T & | last() const |
| void | pop_back() |
| void | pop_front() |
| void | prepend(const T &value) |
| void | push_back(const T &value) |
| void | push_front(const T &value) |
| QLinkedList<T>::reverse_iterator | rbegin() |
| QLinkedList<T>::const_reverse_iterator | rbegin() const |
| int | removeAll(const T &value) |
| void | removeFirst() |
| void | removeLast() |
| bool | removeOne(const T &value) |
| QLinkedList<T>::reverse_iterator | rend() |
| QLinkedList<T>::const_reverse_iterator | rend() const |
| int | size() const |
| bool | startsWith(const T &value) const |
| void | swap(QLinkedList<T> &other) |
| T | takeFirst() |
| T | takeLast() |
| std::list<T> | toStdList() const |
| bool | operator!=(const QLinkedList<T> &other) const |
| QLinkedList<T> | operator+(const QLinkedList<T> &other) const |
| QLinkedList<T> & | operator+=(const QLinkedList<T> &other) |
| QLinkedList<T> & | operator+=(const T &value) |
| QLinkedList<T> & | operator<<(const QLinkedList<T> &other) |
| QLinkedList<T> & | operator<<(const T &value) |
| QLinkedList<T> & | operator=(const QLinkedList<T> &other) |
| bool | operator==(const QLinkedList<T> &other) const |
静态公共成员
| QLinkedList<T> | fromStdList(const std::list<T> &list) |
相关的非成员
| QDataStreamIfHasOStreamOperatorsContainer<QLinkedList<T>, T> | operator<<(QDataStream &out, const QLinkedList<T> &list) |
| QDataStreamIfHasIStreamOperatorsContainer<QLinkedList<T>, T> | operator>>(QDataStream &in, QLinkedList<T> &list) |
详细说明
QLinkedList<T> 是 Qt 的泛型容器类之一,其中T 表示列表中存储的元素类型。它存储一组值,并提供基于迭代器的访问方式,以及常数时间复杂度的插入和删除操作。
QList<T> 和 QLinkedList<T> 提供类似的功能。概述如下:
- 在大多数情况下,QList 是更合适的选择。其基于索引的 API 比 QLinkedList 的基于迭代器的 API 更为便捷。其项占据相邻的内存位置。此外,它在可执行文件中生成的代码量更少。
- 如果您需要真正的链表,且需要保证在列表中间进行常数时间插入,并使用指向项的迭代器而非索引,请使用 QLinkedList。
以下是一个存储整数的 QLinkedList 示例,以及一个存储QTime 值的 QLinkedList 示例:
QLinkedList<int> integerList;
QLinkedList<QTime> timeList;QLinkedList 存储一组项目。默认构造函数会创建一个空列表。要向列表中插入项目,可以使用 operator<<():
QLinkedList<QString> list;
list << "one" << "two" << "three";
// list: ["one", "two", "three"]若要获取链表的首项或末项,请使用 `first()` 或 `last()`。若要从链表的两端删除项,请使用 `removeFirst()` 或 `removeLast()`。若要删除链表中所有出现某特定值的项,请使用 `removeAll()`。
一个常见的需求是移除列表的首项或末项,并对该项进行处理。为此,QLinkedList 提供了takeFirst() 和takeLast() 方法。以下是一个循环,它逐个从列表中移除项,并对它们调用delete :
QLinkedList<QWidget *> list;
...
while (!list.isEmpty())
delete list.takeFirst();QLinkedList 的值类型必须是可赋值的数据类型。 这涵盖了大多数常用数据类型,但编译器不允许您将 QWidget 作为值存储;相反,应存储 QWidget *。部分函数有额外要求;例如,contains() 和removeAll() 要求值类型支持operator==() 。这些要求会在每个函数的文档中详细说明。
若要在列表中间插入、修改或删除项目,必须使用迭代器。QLinkedList 同时提供了Java 风格的迭代器(QLinkedListIterator 和QMutableLinkedListIterator )以及STL 风格的迭代器(QLinkedList::const_iterator 和QLinkedList::iterator )。详情请参阅这些类的文档。
另请参阅 QLinkedListIterator 、QMutableLinkedListIterator 和QList 。
成员类型文档
QLinkedList::ConstIterator
QLinkedList::const_iterator 的 Qt 风格同义词。
QLinkedList::Iterator
QLinkedList::iterator 的Qt风格同义词。
QLinkedList::const_pointer
const T * 的 typedef。为兼容 STL 而提供。
QLinkedList::const_reference
const T & 的 typedef。为兼容 STL 而提供。
QLinkedList::const_reverse_iterator
QLinkedList::const_reverse_iterator 类型别名(typedef)为 `QLinkedList` 提供了一个 STL 风格的 const 反向迭代器。
它只是 `std::reverse_iterator<QLinkedList::const_iterator>` 的一个 typedef。
警告: 在隐式共享容器上的迭代器其 工作方式与 STL 迭代器并不完全一致。当迭代器在容器上处于活动状态时,应避免复制该容器。有关更多信息,请参阅《隐式共享迭代器问题》。
另请参阅 QLinkedList::rbegin()、QLinkedList::rend()、QLinkedList::reverse_iterator 以及QLinkedList::const_iterator 。
QLinkedList::difference_type
ptrdiff_t 的 typedef 定义。为兼容 STL 而提供。
QLinkedList::pointer
T * 的 typedef。为兼容 STL 而提供。
QLinkedList::reference
T 的 typedef 定义。为确保与 STL 兼容而提供。
QLinkedList::reverse_iterator
QLinkedList::reverse_iterator 类型别名(typedef)为 `QLinkedList` 提供了一个 STL 风格的非 const 反向迭代器。
它只是 `std::reverse_iterator<QLinkedList::iterator>` 的一个 typedef。
警告: 隐式共享容器上的迭代器其 行为与 STL 迭代器并不完全一致。应避免在迭代器处于活动状态时复制该容器。有关更多信息,请参阅《隐式共享迭代器问题》。
另请参阅 QLinkedList::rbegin()、QLinkedList::rend()、QLinkedList::const_reverse_iterator 以及QLinkedList::iterator 。
QLinkedList::size_type
int 的 typedef。为兼容 STL 而提供。
QLinkedList::value_type
T 的 typedef 定义。为确保与 STL 兼容而提供。
成员函数文档
[noexcept] QLinkedList::QLinkedList()
创建一个空列表。
QLinkedList::QLinkedList(std::initializer_list<T> list)
根据list 指定的std::initializer_list构建一个列表。
只有当编译器支持 C++11 初始化列表时,此构造函数才可用。
template <typename InputIterator, QtPrivate::IfIsInputIterator<InputIterator> = true> QLinkedList::QLinkedList(InputIterator first, InputIterator last)
构建一个包含迭代器范围 [first,last) 内内容的列表。
InputIterator 的值类型必须可转换为T 。
QLinkedList::QLinkedList(const QLinkedList<T> &other)
创建other 的副本。
由于 QLinkedList 是隐式共享的,因此该操作在常数时间内完成。这使得从函数中返回 QLinkedList 非常快速。如果共享的实例被修改,则会进行复制(写时复制),而此操作耗时为线性时间。
另请参阅 operator=()。
[noexcept] QLinkedList::QLinkedList(QLinkedList<T> &&other)
通过移动构造创建一个 QLinkedList 实例,使其指向与 `other ` 所指向的同一对象。
QLinkedList::~QLinkedList()
销毁该列表。对列表中值的引用以及所有遍历该列表的迭代器都将失效。
void QLinkedList::append(const T &value)
将value 插入列表末尾。
示例:
QLinkedList<QString> list;
list.append("one");
list.append("two");
list.append("three");
// list: ["one", "two", "three"]这与 list.insert(end(),value) 效果相同。
另请参阅 operator<<()、prepend() 和insert()。
T &QLinkedList::back()
提供此函数是为了兼容 STL。它等同于 `last()`。
const T &QLinkedList::back() const
这是一个重载函数。
QLinkedList<T>::iterator QLinkedList::begin()
返回一个指向列表中第一个元素的STL 风格迭代器。
另请参阅 constBegin() 和end()。
[noexcept] QLinkedList<T>::const_iterator QLinkedList::begin() const
这是一个重载函数。
[noexcept] QLinkedList<T>::const_iterator QLinkedList::cbegin() const
返回一个指向列表中第一个元素的常量STL 风格迭代器。
[noexcept] QLinkedList<T>::const_iterator QLinkedList::cend() const
返回一个指向列表中最后一个元素之后那个“虚拟”元素的常量STL 风格迭代器。
void QLinkedList::clear()
删除列表中的所有项目。
另请参阅 removeAll()。
[noexcept] QLinkedList<T>::const_iterator QLinkedList::constBegin() const
返回一个指向列表中第一个元素的常量STL 风格迭代器。
[noexcept] QLinkedList<T>::const_iterator QLinkedList::constEnd() const
返回一个指向列表中最后一个元素之后的虚拟元素的、STL 风格的const迭代器。
另请参阅 constBegin() 和end()。
bool QLinkedList::contains(const T &value) const
如果列表中包含value ,则返回true ;否则返回false 。
此函数要求值类型实现了operator==() 接口。
另请参阅 QLinkedListIterator::findNext() 和QLinkedListIterator::findPrevious()。
int QLinkedList::count() const
与size() 相同。
int QLinkedList::count(const T &value) const
返回列表中value 的出现次数。
此函数要求该值类型实现了 `operator==()` 方法。
另请参阅 contains()。
[noexcept] QLinkedList<T>::const_reverse_iterator QLinkedList::crbegin() const
返回一个指向列表中第一个元素(按反向顺序)的常量STL 风格反向迭代器。
另请参阅 begin()、rbegin() 和rend()。
[noexcept] QLinkedList<T>::const_reverse_iterator QLinkedList::crend() const
返回一个常量STL 风格的反向迭代器,该迭代器指向列表中最后一个元素之后的一个位置,且按反向顺序排列。
bool QLinkedList::empty() const
提供此函数是为了与 STL 兼容。它等同于 `isEmpty()`,如果列表为空,则返回 `true `。
QLinkedList<T>::iterator QLinkedList::end()
返回一个STL 风格的迭代器,该迭代器指向列表中最后一个元素之后的虚拟元素。
[noexcept] QLinkedList<T>::const_iterator QLinkedList::end() const
这是一个重载函数。
bool QLinkedList::endsWith(const T &value) const
如果列表不为空且其最后一个元素等于value ,则返回true ;否则返回false 。
QLinkedList<T>::iterator QLinkedList::erase(QLinkedList<T>::iterator pos)
从列表中移除迭代器pos 所指向的元素,并返回指向列表中下一个元素的迭代器(该迭代器可能是end())。
另请参阅 insert()。
QLinkedList<T>::iterator QLinkedList::erase(QLinkedList<T>::iterator begin, QLinkedList<T>::iterator end)
从begin 到end (不包括 )之间,移除所有项目。
这是一个重载函数。
T &QLinkedList::first()
返回列表中第一个元素的引用。该函数假设列表不为空。
const T &QLinkedList::first() const
这是一个重载函数。
[static] QLinkedList<T> QLinkedList::fromStdList(const std::list<T> &list)
返回一个QLinkedList 对象,其中包含list 中的数据。QLinkedList 中元素的顺序与list 中的顺序一致。
示例:
std::list<double> stdlist;
list.push_back(1.2);
list.push_back(0.5);
list.push_back(3.14);
QLinkedList<double> list = QLinkedList<double>::fromStdList(stdlist);另请参阅 toStdList()。
T &QLinkedList::front()
提供此函数是为了兼容 STL。它等同于 `first()`。
const T &QLinkedList::front() const
这是一个重载函数。
QLinkedList<T>::iterator QLinkedList::insert(QLinkedList<T>::iterator before, const T &value)
在迭代器before 所指向的元素前面插入元素value 。返回一个指向已插入元素的迭代器。
另请参阅 erase()。
bool QLinkedList::isEmpty() const
如果列表中没有元素,则返回true ;否则返回false。
另请参阅 size()。
T &QLinkedList::last()
返回列表中最后一个元素的引用。该函数假设列表不为空。
另请参阅 ` first()` 和 `isEmpty()`。
const T &QLinkedList::last() const
这是一个重载函数。
void QLinkedList::pop_back()
提供此函数是为了兼容 STL。它等同于 `removeLast()`。
void QLinkedList::pop_front()
提供此函数是为了兼容 STL。它等同于 `removeFirst()`。
void QLinkedList::prepend(const T &value)
将value 插入列表开头。
示例:
QLinkedList<QString> list;
list.prepend("one");
list.prepend("two");
list.prepend("three");
// list: ["three", "two", "one"]这与 list.insert(begin(),value) 的效果相同。
void QLinkedList::push_back(const T &value)
提供此函数是为了兼容 STL。它等同于 append(value)。
void QLinkedList::push_front(const T &value)
提供此函数是为了兼容 STL。它等同于 prepend(value)。
QLinkedList<T>::reverse_iterator QLinkedList::rbegin()
返回一个STL 风格的反向迭代器,该迭代器指向列表中第一个元素,且顺序为反向。
另请参阅 begin()、crbegin() 和rend()。
[noexcept] QLinkedList<T>::const_reverse_iterator QLinkedList::rbegin() const
这是一个重载函数。
int QLinkedList::removeAll(const T &value)
从列表中删除所有value 的实例。
示例:
QList<QString> list;
list << "sun" << "cloud" << "sun" << "rain";
list.removeAll("sun");
// list: ["cloud", "rain"]此函数要求值类型已实现operator==() 接口。
另请参阅 insert()。
void QLinkedList::removeFirst()
删除列表中的第一个项目。
这与 erase(begin()) 效果相同。
另请参阅 removeLast() 和erase()。
void QLinkedList::removeLast()
删除列表中的最后一个项目。
另请参阅 removeFirst() 和erase()。
bool QLinkedList::removeOne(const T &value)
从列表中移除第一个出现的“value ”。成功时返回true ;否则返回false 。
示例:
QList<QString> list;
list << "sun" << "cloud" << "sun" << "rain";
list.removeOne("sun");
// list: ["cloud", "sun", "rain"]此函数要求值类型实现了operator==() 接口。
另请参阅 insert()。
QLinkedList<T>::reverse_iterator QLinkedList::rend()
返回一个STL 风格的反向迭代器,该迭代器指向列表中最后一个元素之后的一个位置,且按反向顺序排列。
[noexcept] QLinkedList<T>::const_reverse_iterator QLinkedList::rend() const
这是一个重载函数。
int QLinkedList::size() const
返回列表中的项目数量。
bool QLinkedList::startsWith(const T &value) const
如果列表不为空且其第一个元素等于value ,则返回true ;否则返回false 。
[noexcept] void QLinkedList::swap(QLinkedList<T> &other)
将列表other 与该列表进行交换。此操作速度极快,且从不失败。
T QLinkedList::takeFirst()
移除列表中的第一个元素并返回该元素。
如果不使用返回值,removeFirst() 的执行效率更高。
另请参阅 takeLast() 和removeFirst()。
T QLinkedList::takeLast()
移除列表中的最后一个元素并返回该元素。
如果不需要使用返回值,使用removeLast() 会更高效。
另请参阅 takeFirst() 和removeLast()。
std::list<T> QLinkedList::toStdList() const
返回一个包含此QLinkedList 中数据的std::list对象。示例:
QLinkedList<double> list;
list << 1.2 << 0.5 << 3.14;
std::list<double> stdlist = list.toStdList();另请参阅 fromStdList()。
bool QLinkedList::operator!=(const QLinkedList<T> &other) const
如果 `other ` 与该列表不相同,则返回 `true `;否则返回 `false`。
如果两个列表包含相同的值且顺序相同,则视为相等。
此函数要求值类型实现operator==() 。
另请参阅 operator==()。
QLinkedList<T> QLinkedList::operator+(const QLinkedList<T> &other) const
返回一个列表,其中包含该列表中的所有项目,后接other 列表中的所有项目。
另请参阅 operator+=()。
QLinkedList<T> &QLinkedList::operator+=(const QLinkedList<T> &other)
将other 列表中的项目追加到此列表中,并返回对此列表的引用。
QLinkedList<T> &QLinkedList::operator+=(const T &value)
将value 追加到列表中。
这是一个重载函数。
QLinkedList<T> &QLinkedList::operator<<(const QLinkedList<T> &other)
将other 列表中的项目追加到该列表中,并返回该列表的引用。
另请参阅 operator+=() 和append()。
QLinkedList<T> &QLinkedList::operator<<(const T &value)
将value 追加到列表中。
这是一个重载函数。
QLinkedList<T> &QLinkedList::operator=(const QLinkedList<T> &other)
将other 赋值给此列表,并返回对此列表的引用。
bool QLinkedList::operator==(const QLinkedList<T> &other) const
如果 `other ` 与该列表相等,则返回 `true `;否则返回 `false`。
如果两个列表包含相同的值且顺序相同,则认为它们相等。
此函数要求值类型实现operator==() 接口。
另请参阅 operator!=()。
相关的非成员
template <typename T> QDataStreamIfHasOStreamOperatorsContainer<QLinkedList<T>, T> operator<<(QDataStream &out, const QLinkedList<T> &list)
将链表list 写入流out 。
此函数要求值类型实现operator<<() 接口。
另请参阅 QDataStream 运算符的格式。
template <typename T> QDataStreamIfHasIStreamOperatorsContainer<QLinkedList<T>, T> operator>>(QDataStream &in, QLinkedList<T> &list)
从流in 中读取链表,并将其读入list 中。
此函数要求值类型实现operator>>() 接口。
另请参阅 QDataStream 运算符的格式。
© 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.