tags:

views:

1817

answers:

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:

  • Qt containers can be passed along to other parts of Qt. For example, they can be used to populate a QVariant and then a QSettings (with some limitation though, only QList and QMap/QHash whose keys are strings are accepted).

Is there any other?

Edit: Assuming the application already relies on Qt.

+8  A: 

Qt containers use copy-on-write idiom.

TimW
+1, might be a significant advantage in performance and resource
RedGlyph
Or might be a significant disadvantage. See http://www.gotw.ca/publications/optimizations.htm
Kaz Dragon
The atomic refcount seems to fare pretty well: http://labs.trolltech.com/blogs/2006/10/16/atomic-reference-counting-is-it-worth-it-2/
rpg
+1  A: 
AlexKR
STL containers are all similar, irrespective of the implementation. You can't have a vector implemented like a list behind the scenes as it has to be in a contiguous block of memory. The STL is also usually optimized to huge extents on all major platforms.
Yacoby
If you stick to what the STL promises (instead of assuming how it's implemented) you'll never have a problem moving between platforms with STL. Same with Qt.
Michael Kohne
+45  A: 

This is a difficult to answer question. It can really boil down to a philosophical/subjective argument.

That being said...

I recommend the rule "When in Rome... Do as the Romans Do"

Which means if you are in Qt land, code as the Qt'ians do. This is not just for readability/consistency concerns. Consider what happens if you store everything in a stl container then you have to pass all that data over to a Qt function. Do you really want to manage a bunch of code that copies things into/out-of Qt containers. Your code is already heavily dependent on Qt, so its not like you're making it any more "standard" by using stl containers. And whats the point of a container if everytime you want to use it for anything useful, you have to copy it out into the corresponding Qt container?

Doug T.
Excellent answer!
Igor Oks
+1 you totally right, that's what I tried to explain in my question ("I can see one reason to prefer Qt") so I edited it slightly. Thanks
Julien L.
+5  A: 

Besides the COW difference, STL containers are much more widely supported on a variety of platforms. Qt is portable enough if you limit your work to "mainstream" platforms, but the STL is available on many other more obscure platforms too (e.g., Texas Instruments' DSPs).

Because the STL is standard rather than controlled by a single corporation, there are, generally speaking, more programmers who can easily read, understand, and modify STL code and more resources (books, online forums, conferences, etc.) to support them in doing this than there are for Qt. That's not to say that one should shy away from Qt for this reason alone; just that, all other things being equal, you should default to the STL, but of course all things are rarely equal, so you'll have to decide in your own context which makes the most sense.

In regard to AlexKR's answer: the STL performance is guaranteed within limits, but a given implementation may make use of platform-dependent details to speed up their STL. So in that sense, you may get different results on different platforms, but it will never be slower than the explicit guarantee (modulo bugs).

mlimber
With regards to your first point: I assume the OP is referring to projects that already use Qt, and are therefore already limited to "mainstream" platforms. It seems unlikely that someone would pull in such a heavyweight library as Qt just for its container classes.
ThisSuitIsBlackNot
+20  A: 

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 metatypes 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 accross 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.

rpg
+5  A: 

One of the main issues is that Qt's API expects you to provide data in Qt's containers, so you may as well simply use the Qt containers rather than transforming back and forth between the two.

Also, if you're already using the Qt containers, it might be slightly more optimal to use them exclusively, as you would not have to include the STL header files and potentially link in the STL libraries. However, depending on your toolchain, that may happen anyway. Purely from a design perspective, consistency is generally a good thing.

qid
A: 

I guess it depends on the way you use Qt. If you use it all over your product, than it probably makes sense to use Qt containers. If you contain it only to (for instance) the UI portion, it may be better to use C++ standard containers.

Nemanja Trifunovic
+4  A: 

STL containers:

  • Have performance guarantees
  • Can be used in STL algorithms which also have performance guarantees
  • Can be leveraged by third-party C++ libraries like Boost
  • Are standard, and likely to outlive proprietary solutions
  • Encourage generic programming of algorithms and data structures. If you write new algorithms and data structures that conform to STL you can leverage what STL already provides at no cost.
fbrereto
All of the above except being a standard are true for the QTL also, provided that you compile Qt with STL support (the default). STL support includes iterator functions, container typedefs (const_iterator, etc), conversion functions (to/from STL).
rpg
A: 

I am of the opinion that STL is a excellent piece of software however if I am to do some KDE or Qt related programming then Qt is the way to go. Also it depends on the compiler you are using, with GCC STL works pretty good however if you have to use say SUN Studio CC then STL will most likely bring you headaches because of the compiler not the STL per se. In that case since the compiler will make your head hurt just use Qt to save you the trouble. Just my 2 cents...

Paulo Lopes
+2  A: 

If the data you are working with is mostly used to drive the Qt based UI, then definitely use Qt containers.

If the data is mostly used internally in the app, and you're never likely to port away from Qt, then barring performance issues, use the Qt containers because it will make the bits of data that go to the UI easier to deal with.

If the data is mostly used in conjunction with other libraries that only know about STL containers, then use STL containers. If you have this situation you're in trouble no matter what because you're going to do a lot of porting back and forth between container types no matter what you do.

Michael Kohne