QQueue Class
template <typename T> class QQueueQQueue 类是一个提供队列功能的泛型容器。更多内容...
| 头文件: | #include <QQueue> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
| 继承自: | QList |
- 所有成员列表(包括继承的成员)
- QQueue 属于“隐式共享类”和“容器类”。
注意:该类中的所有函数均为可重入的。
公共函数
| T | dequeue() |
| void | enqueue(T &&t) |
| void | enqueue(const T &t) |
| T & | head() |
| const T & | head() const |
| void | swap(QQueue<T> &other) |
详细说明
QQueue<T> 是 Qt 的泛型容器类之一,其中T 指定了元素类型。它为同类型的项目实现了队列数据结构。
队列是一种先入先出(FIFO)结构。使用enqueue() 将项目添加到队列尾部,使用dequeue() 从队列头部取出项目。head() 函数允许访问队列头部的项目而不将其移除。
示例:
QQueue<int> queue;
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
while (!queue.isEmpty())
cout << queue.dequeue() << endl;该示例将按顺序输出 1、2、3。
QQueue 继承自QList 。QList 的所有功能也适用于 QQueue。例如,您可以使用isEmpty() 来检测队列是否为空,并且可以使用QList 的迭代器类(例如QListIterator )遍历一个 QQueue。 此外,QQueue 还提供了三个便利函数,可轻松实现 FIFO 语义:enqueue()、dequeue() 和head()。
QQueue 的值类型必须是可赋值的数据类型。这涵盖了大多数常用数据类型,但编译器不允许将QWidget 作为值存储。请改用QWidget*。
成员函数文档
T QQueue::dequeue()
从队列中移除头元素并返回该元素。该函数假设队列不为空。
这与QList::takeFirst() 功能相同。
另请参阅 head()、enqueue() 和isEmpty()。
将值t 添加到队列的末尾。
这与QList::append() 效果相同。
T &QQueue::head()
返回对队列头元素的引用。该函数假设队列不为空。
这与QList::first() 相同。
另请参阅 dequeue()、enqueue() 和isEmpty()。
const T &QQueue::head() const
这是一个重载函数。
[noexcept] void QQueue::swap(QQueue<T> &other)
将该队列与other 互换。此操作速度极快,且绝不会失败。
© 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.