STL或Qt容器? [英] STL or Qt containers?

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

问题描述

使用Qt容器( QMap QVector 等)的优点和缺点等于?

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的其他部分。例如,它们可以用于填充 QVariant ,然后填充 QSettings (尽管有一些限制,但只有 QList QMap / 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容器但是我已经切换到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感知。它还提供

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宏
    (它做一个副本)和使用元类型或信号和插槽时,这是非常有用的。 li>
  • 可以使用STL式迭代器或Java式迭代器

  • 可以使用QDataStream进行流式处理

  • Qt的API

  • 在操作系统上具有稳定的实施。 STL实现必须遵守C ++标准,但是
    可以随意做,因为它喜欢(参见std :: string COW争议)。某些STL实现特别是
    bad。

  • 提供散列,除非您使用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 metatypes 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 accross 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有不同的理念,即井摘要:虽然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容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆