qt - QQueue容器存在的目的是什么?

标签 qt qlist

我从那时起就开始使用 Qt,并且一直在使用 QQueue类作为 std::queue 的替代品.虽然今天我认为我能够使用 pop_front , pop_back , push_frontpush_back来自 QList 的函数QQueue 对象上的类。想知道为什么可以在队列上使用这些函数(尤其是 pop_backpush_front),我查看了 Qt 文档,发现 QQueue 继承了 QList。

对我来说,队列的主要兴趣在于充当队列:在后面入队,在前面出队。但是当我继续阅读文档时,我发现 enqueuedequeue功能相当于appendtakeFirst QList类的功能。即使是 head功能与 first 相同QList的功能。最重要的是,使用pop_backpush_front 等函数的可能性对我来说完全打破了队列的初始逻辑,而文档本身将 QQueue 类呈现为队列容器:

The QQueue class is a generic container that provides a queue.

QQueue is one of Qt's generic container classes. It implements a queue data structure for items of a same type.

A queue is a first in, first out (FIFO) structure. Items are added to the tail of the queue using enqueue() and retrieved from the head using dequeue(). The head() function provides access to the head item without removing it.

关于 QQueue 类存在的目的,我是否遗漏了什么?我的意思是,为什么要创建这个队列容器,以便它可以做比队列应该做的更多的事情?由于 QQueue 类似乎能够以与 QList 完全相同的方式工作,是否有任何理由在 QList 上使用 QQueue?

最佳答案

containers description 中所述:

QStack and QQueue are convenience classes that provide LIFO and FIFO semantics.

唯一的区别是您提到的其他方法:

重点是语义上更合适且直观的方法命名

否则:

  • QQueue 与其基类 QList 相同。
  • QStack 与其基类 QVector 相同。

自己看看吧,看看源代码——这些类中没有任何额外内容:

关于qt - QQueue容器存在的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43325750/

相关文章:

c++ - Qt C++初始化列表困惑

c++ - 如何将 txt 文件中的值插入 Qlist 并使用 QTablewideget

c++ - 递归走目录问题

c++ - 具有写时复制的多态类的 QList?

c++ - 对象拥有 QObject 派生类的集合的正确方法是什么?

c++ - 你如何处理内存管理和信号/插槽?

c++ - Foreach 通过 QJsonObject 获取 Key/Value Pair

c++ - 如何在 VS 2013 中使用 QT 4.8.x?

linux - 如何在 Qt5.5 中使用 gstreamer1.0 而不是 0.10?

c++ - Qt QList 没有按预期工作