I have used Qt 5.15 for some months to do some basic stuff.
In Qt5, it's known that QList, and QVector are 2 different containers:
QList will store its raw data continuously in memory (as long as the size of each element is <= sizeof(void*)); otherwise, it will store a continuous chunk of pointers pointing to the actual data in the heap.
QVector, on the other hand, will store its data continuously in memory.
And in Qt6, QList and also QVector will be merged; meaning now QList (in Qt6) will behave exactly like QVector in (Qt5); and the original QList (Qt5) will be dropped.
It's easy to see that in Qt 5 QList<int>
should be the same as QVector<int>
; meanwhile, QList<QByteArray>
, QVector<QByteArray>
; or QList<QString>
, QVector<QString>
should act totally different (?) because, as I understand it, QList will store pointers (and use heap to store data), meanwhile QVector will store raw data from all elements.
So, say (I'm still talking about Qt5), I have QVector<QString> a("")
;
Then I append, and do stuff to it to get like 100 elements from a.at(0) to a.at(99). If I want to retrieve the 50th data, I'll do a.at(49); but how can my program retrieve the data? Will it read from the beginning of the chunk, until it hits the 50th element? If it does so, then the speed to retrieve data is not O(1); and it's not fast at all; contradicting its description here:
https://doc-snapshots.qt.io/qt6-dev/qlist.html#details
QList is one of Qt's generic container classes. It stores its items in adjacent memory locations and provides fast index-based access.
I know that if it were a Qt5 QList, then it should just offset the memory from the start to get the pointer to my 50th element, and read the raw data stored at the address pointed to by the 50th element pointer stored in QList; which will take O(1) time (and this is fast).
My question is, in Qt6, how can QList<QString>
be considered to provide fast index-based access in this situation? Am I missing something here?
Thank you very much in advance, :D
QByteArray
and QString
themselves won't contain any data, just a pointer to a dynamically allocated array (try running sizeof(QByteArray)
and sizeof(QString)
, you'll get a small constant size for both).
All instances of a the same class in C++ have the same size so in an array of objects accessing an index is just a case of seeking to element_size * index
bytes from the beginning of the array.