I use QList
s all over my code. According to https://doc.qt.io/qt-5/qvector.html and the linked page http://marcmutz.wordpress.com/effective-qt/containers/ QVector
"should always be the first choice", or one shouldn't use QList
at all, as long as one doesn't handle Qt API functions taking or returning a QList
.
So I now think about to replace all the QList
s and QStringList
s in my code with QVector
s (which should be no problem at all).
But what is the most efficient way to fill a QVector
with data? There are three options:
QVector<sometype> vector;
for (whatever) {
vector.append(someting)
}
This it what I currently do with most of my QList
s. But often, I know how many elements the vector will have, and a QVector
can be initialized with a given length or told how many elements it will have. So I can either do:
QVector<sometype> vector(count);
int i = 0;
for (whatever) {
vector.insert(i++, something);
}
or:
QVector<sometype> vector;
vector.reserve(count);
for (whatever) {
vector.append(something);
}
So which one is the most efficient approach?