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:
- Qt containers can be passed along to other parts of Qt. For example, they can be used to populate a
QVariantand then aQSettings(with some limitation though, onlyQListandQMap/QHashwhose keys are strings are accepted).
Is there any other?
Edit: Assuming the application already relies on Qt.
I started by using
std::(w)stringand the STL containers exclusively and converting to/from the Qt equivalents, but I have already switched toQStringand I find that I’m using Qt’s containers more and more.When it comes to strings,
QStringoffers much more complete functionality compared tostd::basic_stringand it iscompletely Unicode aware. It also offers an efficient COW implementation, which I’ve come to rely on heavily.
Qt’s containers:
QString, which is extremely useful when it comes to using Qt’sforeachmacro(which does a copy) and when using meta-types or signals and slots.
QDataStreamis otherwise free to do as it pleases (see the
std::stringCOW controversy). Some STL implementations are especiallybad.
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.