[Interest] QVectors vs QLists
Sergio Martins
sergio.martins at kdab.com
Wed May 3 19:14:38 CEST 2017
On 2017-05-03 17:57, Jason H wrote:
> I have a app that does a lot of numeric processing,using quint32,
> qreal mostly (and occasional QVector3D). 10k-1M items in the data set.
> I generally do know the size of the dataset before I start adding to
> it, so I call reserve() on it. Given that, and that I've been using
> QMap and QVector, when I use QMap and want the keys() it gives me a
> QList, which I then have to call toVector() on.
You can easily construct a QVector of keys by iterating the map, using
QMap::keyBegin() and QMap::keyEnd(). Don't use toVector().
> So this leads me to ask if I should be using QList all this time? I
> had thought that QList was considered harmful. [1][2][3]
> But "everything" seems to return to me a QList, rather than a QVector.
It still returns QList because it can't be changed during the Qt 5
lifetime.
> What should I be using? (I'm Targeting 5.8 and later.)
The question should maybe be "What shouldn't I be using".
Are you user you need QMap::keys() ? Each time you call it it will
instantiate a new container.
Read point number 5 (container-anti-pattern) of
https://www.kdab.com/uncovering-32-qt-best-practices-compile-time-clazy/.
Sure, sometimes you'll need it. But if it's just for iteration purposes
you can iterate the map directly.
Regards,
--
SĂ©rgio Martins | sergio.martins at kdab.com | Senior Software Engineer
Klarälvdalens Datakonsult AB, a KDAB Group company
Tel: Sweden (HQ) +46-563-540090, USA +1-866-777-KDAB(5322)
KDAB - The Qt, C++ and OpenGL Experts
More information about the Interest
mailing list