STL 还是 Qt 容器?

STL or Qt containers?(STL 还是 Qt 容器?)

本文介绍了STL 还是 Qt 容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Qt 容器(QMapQVector 等)与其等效的 STL 相比有哪些优缺点?

What are the pros and cons of using Qt containers (QMap, QVector, etc.) over their STL equivalent?

我可以看到一个喜欢 Qt 的原因:

I can see one reason to prefer Qt:

  • Qt 容器可以传递到 Qt 的其他部分.例如,它们可用于填充 QVariantQSettings(但有一些限制,只有 QListQMap/QHash 其键是字符串被接受).
  • Qt containers can be passed along to other parts of Qt. For example, they can be used to populate a QVariant and then a QSettings (with some limitation though, only QList and QMap/QHash whose keys are strings are accepted).

还有吗?

编辑:假设应用程序已经依赖于 Qt.

Edit: Assuming the application already relies on Qt.

推荐答案

我开始时只使用 std::(w)string 和 STL 容器并转换为/从 Qt 等效项,但是我已经切换到 QString 并且我发现我越来越多地使用 Qt 的容器.

I started by using std::(w)string and the STL containers exclusively and converting to/from the Qt equivalents, but I have already switched to QString and I find that I'm using Qt's containers more and more.

当谈到字符串时,QString 提供了比 std::basic_string 更完整的功能,而且它是完全 Unicode 感知.它还提供了一个 高效的 COW 实施,这是我非常依赖的.

When it comes to strings, QString offers much more complete functionality compared to std::basic_string and it is completely Unicode aware. It also offers an efficient COW implementation, which I've come to rely on heavily.

Qt 的容器:

  • 提供与 QString 相同的 COW 实现,这在使用 Qt 的 foreach 宏时非常有用(进行复制)以及使用元类型或信号和槽时.
  • 可以使用 STL 风格的迭代器或 Java 风格的迭代器
  • 可以使用 QDataStream
  • 流式传输
  • 在 Qt 的 API 中被广泛使用
  • 在各种操作系统上都有稳定的实现.STL 实现必须遵守 C++ 标准,但否则可以随心所欲(参见 std::string COW 争议).一些 STL 实现特别是不好.
  • 提供哈希值,除非使用 TR1 否则无法使用
  • offer the same COW implementation as in QString, which is extremely useful when it comes to using Qt's foreach macro (which does a copy) and when using meta-types or signals and slots.
  • can use STL-style iterators or Java-style iterators
  • are streamable with QDataStream
  • are used extensively in Qt's API
  • have a stable implementation across operating systems. A STL implementation must obey the C++ standard, but is otherwise free to do as it pleases (see the std::string COW controversy). Some STL implementations are especially bad.
  • provide hashes, which are not available unless you use TR1

QTL 与 STL 有不同的哲学,这是总结得很好 作者:J. Blanchette:虽然 STL 的容器针对原始速度进行了优化,但 Qt 的容器类经过精心设计,以提供便利、最少的内存使用和最少的代码扩展."
上面的链接提供了有关 QTL 实现和使用了哪些优化的更多详细信息.

The QTL has a different philosophy from the STL, which is well summarized by J. Blanchette: "Whereas STL's containers are optimized for raw speed, Qt's container classes have been carefully designed to provide convenience, minimal memory usage, and minimal code expansion."
The above link provides more details about the implementation of the QTL and what optimizations are used.

这篇关于STL 还是 Qt 容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:STL 还是 Qt 容器?

基础教程推荐