Quiz Time! Prepending items to QVector
-
How many elements?
The benchmark tested 10, 50, 100, 500, 1000, 5000 but turns out the answer doesn't change with size
can I use reserve?
Yes, in both cases I first called
QVector::reserve
Are we talking 32 or 64 bits?
64
I would expect using a reverse iterator for reading and append for writing would beat both strategies mentioned in the initial post.
Yes, this is obvious but in my use case I can't read with reverse iterator
or does this also need to have the initial sequence reversed prior to the append + reverse operation?
To make it more explicit: If I have the sequence
1, 3, 2, 5
(that I can't reverse iterate) I want the vector to store those as5, 2, 3, 1
-
Is there any article about these specific changes in Qt 6 that I can read? What exactly has changed between Qt 5 and 6?
-
@SimonSchroeder said in Quiz Time! Prepending items to QVector:
What exactly has changed between Qt 5 and 6?
QVector is no longer in Qt6 and an alias for QList. I would guess QList in Qt5 will also show the same fast results for prepend since QList has an optimization for this usecase since ages.
-
From https://wiki.qt.io/New_Features_in_Qt_6.0
QVector and QList are unified. QList is updated and should be used by default when array-like behaviour is needed
QList, QString and QByteArray now have optimized complexity of insertion at the beginning (a.k.a. prepend) -
@kshegunov said in Quiz Time! Prepending items to QVector:
but what your task sounds like is actually a stack, not a vector
Indeed, this was an iper-semplification of my problem to solve the academic dilemma.
I agree that a stack or, as @jeremy_k already pointed out, using reverse iterators would achieve the same