Results 1 to 6 of 6

Thread: Multi Dimensional QList

  1. #1
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Multi Dimensional QList

    Hi, This must be an easy prob,,,, but my head's not working much today, so posting the query

    I want to have a 2D array of integers using QList,
    something like QList< QList<int> > > twoDArray; The main purpose of creating the array thru QList is that i dont have the dimensions at the time of declaration. They have to be populated later.

    Do I have to iterate each and every element of twoDArray and initialize it witht he correct value ?? Will QVector be better ??

    Can someone show a example for the same ?
    Last edited by aamer4yu; 30th March 2007 at 12:24.

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Multi Dimensional QList

    Quote Originally Posted by aamer4yu View Post
    Hi, This must be an easy prob,,,, but my head's not working much today, so posting the query

    I want to have a 2D array of integers using QList,
    something like QList< QList<int> > > twoDArray; The main purpose of creating the array thru QList is that i dont have the dimensions at the time of declaration. They have to be populated later.

    Do I have to iterate each and every element of twoDArray and initialize it witht he correct value ?? Will QVector be better ??

    Can someone show a example for the same ?
    I love QDomDocument .... populated new level any time && any place and grab fast result... binary or text ....

  3. #3
    Join Date
    Aug 2006
    Posts
    44
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Multi Dimensional QList

    By definition, accesses to QList are O(n), and yes, initializing elements will mean visiting every one indivually -- if you need to change them after inserting them to the list.

    Vectors are resizeable, but the cost of resizing can be higher than with a list. That's the root of the problem -- which dimension of performance is most important? Access speed or resizing speed? The constant time access of a vector is invaluable for large data sets.

    Also, remember that a QVector can be initially sized to zero, and you can push elements at the the front or back very much like a list. Presuming even some simplistic optimizations (and I'm betting it's better), adding / removing elements on a QVector won't have the performance penalties that a brain-dead malloc / memcpy / free approach would.

  4. #4
    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 Re: Multi Dimensional QList

    Quote Originally Posted by Eldritch View Post
    By definition, accesses to QList are O(n),
    Not exactly, as QList is not a typical list, but rather a kind of general purpose container.

    See:
    http://doc.trolltech.com/4.2/contain...mic-complexity

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multi Dimensional QList

    but how do I initialise the list to some size....
    let me repeat my question more specifically...

    Say I have the following class -
    Qt Code:
    1. class MyClass
    2. {
    3. QList<QList<int> > 2dArray;
    4. public :
    5. MyClass()
    6. {
    7. // read row col from file....
    8. // set size of Qlist to row and col
    9. }
    10. friend somefunction();
    11. };
    12.  
    13. somefunction()
    14. {
    15. MyClass objMyClass;
    16. for(i=0;i<row;i++)
    17. for(j=0;j<col;j++)
    18. objMyClass.2darray[i][j] = ;// assign some value
    19. }
    To copy to clipboard, switch view to plain text mode 

    here in the myclass i need to know the size of the array in the ctor and allocate the size to the list. so that later when i call any elemnt of the list, i can use it properly... QList gives error if i try to access index out of bounds...

    i know i can use pointers for the problem... but just trying with QList or QVector...

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multi Dimensional QList

    done it atlast...

    in
    MyClass()
    {
    // read row col from file / other class etc
    2dArray = QVector<QList<int> >(rows).toList();
    for(i=0;i<row;i++)
    2dArray[i] = QVector<int>(cols).toList();
    }

    this helps to allocate the space and its working.... not sure if there are any pitfalls...there might be

    any comments ??

Similar Threads

  1. QVariant, QList, QSettings
    By TheKedge in forum Qt Programming
    Replies: 6
    Last Post: 6th March 2007, 15:50
  2. using Qlist with a class
    By Havard in forum Qt Programming
    Replies: 10
    Last Post: 24th February 2007, 19:38
  3. QList crash in destructor
    By mclark in forum Newbie
    Replies: 7
    Last Post: 6th December 2006, 15:27
  4. Accessing QList Objects
    By magikalpnoi in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2006, 20:43
  5. Values not being appended in a QList
    By Kapil in forum Newbie
    Replies: 5
    Last Post: 21st April 2006, 10:20

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.