使用 Qt 容器(QMap
、QVector
等)与其等效的 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:
QVariant
和 QSettings
(但有一些限制,只有 QList
和 QMap
/QHash
其键是字符串被接受).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
宏时非常有用(进行复制)以及使用元类型或信号和槽时.QDataStream
std::string
COW 争议).一些 STL 实现特别是不好.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.QDataStream
std::string
COW controversy). Some STL implementations are especially
bad.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 容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!