STL or Qt containers?

后端 未结 14 1831
再見小時候
再見小時候 2020-12-02 04:10

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

I can see one reason to prefer Qt:

14条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-02 05:08

    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.

    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's containers:

    • 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

    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.

提交回复
热议问题