PDA

View Full Version : when and why should i use these data structures...



nupul
17th April 2006, 08:20
Qt is provides us with a lot of data structures to use. I read through the docs but am still fuzzy about their use and applications:

QByteArray
QBuffer
QCache
QVector
QMap

under what circumstances should one use them?

Thanks

Nupul

jacek
17th April 2006, 13:42
QByteArray

QByteArray can be used to store both raw bytes (including '\0's) and traditional 8-bit '\0'-terminated strings. Using QByteArray is much more convenient than using const char *. Behind the scenes, it always ensures that the data is followed by a '\0' terminator, and uses implicit sharing (copy-on-write) to reduce memory usage and avoid needless copying of data.
It just a smarter version of char *.

QBuffer

The QBuffer class provides a QIODevice interface for a QByteArray.
QBuffer allows you to access a QByteArray using the QIODevice interface. The QByteArray is treated just as a standard random-accessed file.
In other words you can use QBuffer to write and/or read data from/to QByteArray using QTextStream and QDataStream.

QCache

The advantage of using QCache over some other key-based data structure (such as QMap or QHash) is that QCache automatically takes ownership of the objects that are inserted into the cache and deletes them to make room for new objects, if necessary. When inserting an object into the cache, you can specify a cost, which should bear some approximate relationship to the amount of memory taken by the object. When the sum of all objects' costs (totalCost()) exceeds the cache's limit (maxCost()), QCache starts deleting objects in the cache to keep under the limit, starting with less recently accessed objects.

QVector

The QVector class is a template class that provides a dynamic array.
QVector<T> is one of Qt's generic container classes. It stores its items in adjacent memory locations and provides fast index-based access.
It's something like std::vector.

QMap

The QMap class is a template class that provides a skip-list-based dictionary.
QMap<Key, T> is one of Qt's generic container classes. It stores (key, value) pairs and provides fast lookup of the value associated with a key.
You need QMap (or QHash) when the key (i.e. index) isn't an integer.

Brandybuck
17th April 2006, 21:30
QCache is a collection that stores most recently used items. Use it when you need fast access to items but which have an access cost. Usually you will never need one, but there are some special circumstances when they are useful.

QVector is simply an array collection class. This is your standard collection class. In Qt 4.x, QList is also an array, and very similar.

QMap is an associative array. Internally it is represent as a binary tree (whereas QHash is a hash). It is a very useful collection, as you can store items by a key, and very quickly access them.

Which one you use depends on how you want to access your data. If you have numerical sequential indices, an QVector may make sense. If you just need a list of items, a QList is good. QMap is good if you want to look stuff up by name.