PDA

View Full Version : Most elegant Qt Multidimentional array



philwinder
14th June 2008, 00:53
Hi,
What is the most elegant way of creating a Qt-like multidimentional array since I'm having to mess with:

QList<double> myList[100]
and access it with:

myList[x].at(y)

Obviously the x part of the array doesnt benefit from any of Qt's container properties. Any ideas?

JimDaniel
14th June 2008, 05:59
QList<QList<double>> ?

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

philwinder
14th June 2008, 12:57
Yep, Ive just got that working. I was having problems with it because I was trying to do this:

for( int x = 0; x < 181; x++ ){
theList.append(x);
for( int y = 0; y < 2001; y++ )
theList.at(x).append(y);
}
When I should have been doing:

QList<double> temp;
for( int x = 0; x < 181; x++ ){
temp.clear();
for( int y = 0; y < 2001; y++ )
temp.append(y);
theList.append(temp);
}

Cheers,
Phil

wysota
15th June 2008, 10:38
I just came up with a crazy idea to represent a 2D array using a tree or a map:


struct 2DArrayIndex { int x, y; ... };
class 2DArray : public QHash<2DArrayIndex, int> {
//...
};

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