問題描述
使用 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:
- 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 aQSettings
(with some limitation though, onlyQList
andQMap
/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'sforeach
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 容器?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!