Results 1 to 4 of 4

Thread: Most elegant Qt Multidimentional array

  1. #1
    Join Date
    Apr 2008
    Posts
    73
    Thanks
    11
    Thanked 7 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Most elegant Qt Multidimentional array

    Hi,
    What is the most elegant way of creating a Qt-like multidimentional array since I'm having to mess with:
    Qt Code:
    1. QList<double> myList[100]
    To copy to clipboard, switch view to plain text mode 
    and access it with:
    Qt Code:
    1. myList[x].at(y)
    To copy to clipboard, switch view to plain text mode 

    Obviously the x part of the array doesnt benefit from any of Qt's container properties. Any ideas?
    Best Regards,
    Phil Winder
    www.philwinder.com

  2. #2
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Most elegant Qt Multidimentional array

    QList<QList<double>> ?

    double d = myList.at(x).at(y);

  3. #3
    Join Date
    Apr 2008
    Posts
    73
    Thanks
    11
    Thanked 7 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Most elegant Qt Multidimentional array

    Yep, Ive just got that working. I was having problems with it because I was trying to do this:
    Qt Code:
    1. for( int x = 0; x < 181; x++ ){
    2. theList.append(x);
    3. for( int y = 0; y < 2001; y++ )
    4. theList.at(x).append(y);
    5. }
    To copy to clipboard, switch view to plain text mode 
    When I should have been doing:
    Qt Code:
    1. QList<double> temp;
    2. for( int x = 0; x < 181; x++ ){
    3. temp.clear();
    4. for( int y = 0; y < 2001; y++ )
    5. temp.append(y);
    6. theList.append(temp);
    7. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    Phil
    Best Regards,
    Phil Winder
    www.philwinder.com

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Most elegant Qt Multidimentional array

    I just came up with a crazy idea to represent a 2D array using a tree or a map:

    Qt Code:
    1. struct 2DArrayIndex { int x, y; ... };
    2. class 2DArray : public QHash<2DArrayIndex, int> {
    3. //...
    4. };
    To copy to clipboard, switch view to plain text mode 

    One would need a custom method to access the data, so that it returned some default value if the item under specified coordinates would not be present, but apart from that we'd have a nice representation of a sparse 2D array. It wouldn't make sense to do it this way with dense arrays though...

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.