Results 1 to 3 of 3

Thread: when and why should i use these data structures...

  1. #1
    Join Date
    Mar 2006
    Posts
    172
    Thanks
    30
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation when and why should i use these data structures...

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default qRe: when and why should i use these data structures...

    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.

  3. The following 2 users say thank you to jacek for this useful post:

    ankurjain (24th May 2006), nupul (17th April 2006)

  4. #3
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: when and why should i use these data structures...

    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.

  5. The following user says thank you to Brandybuck for this useful post:

    nupul (19th April 2006)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.