問題描述
使用 Qt 容器(QMap
、QVector
等)與其等效的 STL 相比有哪些優(yōu)缺點(diǎn)?
What are the pros and cons of using Qt containers (QMap
, QVector
, etc.) over their STL equivalent?
我可以看到一個(gè)喜歡 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).
還有嗎?
編輯:假設(shè)應(yīng)用程序已經(jīng)依賴于 Qt.
Edit: Assuming the application already relies on Qt.
推薦答案
我開始時(shí)只使用 std::(w)string
和 STL 容器并轉(zhuǎn)換為/從 Qt 等效項(xiàng),但是我已經(jīng)切換到 QString
并且我發(fā)現(xiàn)我越來越多地使用 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.
當(dāng)談到字符串時(shí),QString
提供了比 std::basic_string
更完整的功能,而且它是完全 Unicode 感知.它還提供了一個(gè) 高效的 COW 實(shí)施,這是我非常依賴的.
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 實(shí)現(xiàn),這在使用 Qt 的foreach
宏時(shí)非常有用(進(jìn)行復(fù)制)以及使用元類型或信號和槽時(shí). - 可以使用 STL 風(fēng)格的迭代器或 Java 風(fēng)格的迭代器
- 可以使用
QDataStream
流式傳輸 - 在 Qt 的 API 中被廣泛使用
- 在各種操作系統(tǒng)上都有穩(wěn)定的實(shí)現(xiàn).STL 實(shí)現(xiàn)必須遵守 C++ 標(biāo)準(zhǔn),但否則可以隨心所欲(參見
std::string
COW 爭議).一些 STL 實(shí)現(xiàn)特別是不好. - 提供哈希值,除非使用 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 有不同的哲學(xué),這是總結(jié)得很好 作者:J. Blanchette:雖然 STL 的容器針對原始速度進(jìn)行了優(yōu)化,但 Qt 的容器類經(jīng)過精心設(shè)計(jì),以提供便利、最少的內(nèi)存使用和最少的代碼擴(kuò)展."
上面的鏈接提供了有關(guān) QTL 實(shí)現(xiàn)和使用了哪些優(yōu)化的更多詳細(xì)信息.
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.
這篇關(guān)于STL 還是 Qt 容器?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!