Have you tried QVector<QVector<QString> >?
Have you tried QVector<QVector<QString> >?
Thanks. Indeed I'm aware of that. However, I was unable to figure out how to use it. If someone could show how to initialize and assign 2D array this way, I'd be very thankful. I know this does not work (but why?):
Qt Code:
QVector<QVector<QString> > array[3][3]; array[0][0]=test;To copy to clipboard, switch view to plain text mode
Last edited by timmu; 18th May 2012 at 13:36.
Qt Code:
QVector<QVector<QString> > vectorOfVectorsOfStrings; for(int i = 0; i < 3; i++) //of course you might not want to init the vectors in a loop - this is just an example { QVector<QString> foo; //create a QVector of QStrings foo.push_back("fooo"); foo.push_back("booo"); vectorOfVectorsOfStrings.push_back(foo); //add the created vector as a line in your 2D vector } for(int i = 0; i < vectorOfVectorsOfStrings.size(); i++) { for(int j = 0; j < vectorOfVectorsOfStrings[i].size(); j++) { //do stuff with the QString vectorOfVectorsOfStrings[i][j] } }To copy to clipboard, switch view to plain text mode
If the size of the array is known and fixed you might as well use a simple array.
You cannot size the nested QVectors, i.e. to get a 3x5 array of empty QStrings, without either using a loop:
or a construct like:Qt Code:
// 3x5 array QVector<QVector<QString> > v1(3); for(int outer = 0; outer < v1.size(); ++outer) v1[outer].resize(5); qDebug() << v1;To copy to clipboard, switch view to plain text mode
This second approach does allow initialising all the elements to the same, non default value:Qt Code:
// 3x5 array typedef QVector<QString> inner; typedef QVector<inner> outer; outer v2(3, inner(5)); qDebug() << v2;To copy to clipboard, switch view to plain text mode
Qt Code:
// 3 x 5 "?" outer v3(3, inner(5, "?")); qDebug() << v3;To copy to clipboard, switch view to plain text mode
Qt Code:
QVector<QStringList> matrix{{"foo", "bar", "baz"}, {"hello", "world", "!"}}; qDebug() << "output: " << matrix[0]; //Will output : output: ("foo", "bar", "baz") qDebug() << "output: " << matrix[0][1]; //Will output : "bar"To copy to clipboard, switch view to plain text mode
This is not a 2D array. It is a 1D QVector of 1D QList<QString>. It is not the same as a QVector< QVector< QString > > and there are very important differences in terms of memory usage and performance.QVector<QStringList> matrix{{"foo", "bar", "baz"}, {"hello", "world", "!"}};
From the QList documentation:
If your 2D array is rectangular (i.e. has rows with the same number of columns for each row) then probably the best way is to allocate a 1D QVector< QString >:The QList class is a template class that provides lists.
QList<T> is one of Qt's generic container classes. It stores items in a list that provides fast index-based access and index-based insertions and removals.
QList<T>, QLinkedList<T>, and QVector<T> provide similar APIs and functionality. They are often interchangeable, but there are performance consequences. Here is an overview of use cases:
QVector should be your default first choice. QVector<T> will usually give better performance than QList<T>, because QVector<T> always stores its items sequentially in memory, where QList<T> will allocate its items on the heap unless sizeof(T) <= sizeof(void*) and T has been declared to be either a Q_MOVABLE_TYPE or a Q_PRIMITIVE_TYPE using Q_DECLARE_TYPEINFO. See the Pros and Cons of Using QList for an explanation.
...
Note: QVector and QVarLengthArray both guarantee C-compatible array layout. QList does not. This might be important if your application must interface with a C API.
...
Internally, QList<T> is represented as an array of T if sizeof(T) <= sizeof(void*) and T has been declared to be either a Q_MOVABLE_TYPE or a Q_PRIMITIVE_TYPE using Q_DECLARE_TYPEINFO. Otherwise, QList<T> is represented as an array of T* and the items are allocated on the heap.
The array representation allows very fast insertions and index-based access. The prepend() and append() operations are also very fast because QList preallocates memory at both ends of its internal array. (See Algorithmic Complexity for details).
Note, however, that when the conditions specified above are not met, each append or insert of a new item requires allocating the new item on the heap, and this per item allocation will make QVector a better choice for use cases that do a lot of appending or inserting, because QVector can allocate memory for many items in a single heap allocation.
Note that the internal array only ever gets bigger over the life of the list. It never shrinks. The internal array is deallocated by the destructor and by the assignment operator, when one list is assigned to another.
...
Qt Code:
QVector< QString > stringMatrix; stringMatrix.resize( nRows * nCols );To copy to clipboard, switch view to plain text mode
This results in one contiguous data array and very efficient access to any member in it.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
i want to creat a two-dimensional QVector of QLine which means in front of each line another line:
for(int i=0;i<symZqaq.size();i++)
{
QVector<QLineF> sym;
sym.append(L4);
sym.append(L5);
sym.append(L29);
sym.append(L25);
sym.append(L42);
sym.append(L26);
sym.append(L28);
sym.append(L31);
sym.append(L36);
sym.append(L44);
symZqaq.append(sym);
}
for(int i=0;i<symZqaq.size();i++)
{
for(int j=0;j<symZqaq[i].size();i++)
{
symZqaq.append(L9);
symZqaq.append(L12);
symZqaq.append(L24);
symZqaq.append(L30);
symZqaq.append(L27);
symZqaq.append(L43);
symZqaq.append(L7);
symZqaq.append(L2);
symZqaq.append(L6);
symZqaq.append(L3);
}
}
the problem is in last loop for erreur : no matching function for call to 'QVector<QVector<QLineF> >:ush_back(QLineF&)'
symZqaq.push_back(L9);
any help plz
"symZqaq" is a QVector of QVectors, not a QVector of QLines. You can't push a QLine onto symZqaq, only a QVector< QLine >. You did it correctly in the first loop. Use the same method in the nested loop.no matching function for call to 'QVector<QVector<QLineF> >:: push_back(QLineF&)'
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
Bookmarks