Qt line builder for loop

after this and this documentation, I would use QStringBuilder in a for loop. The code in which I should apply it is

QStringList words; QString testString; for (auto it = words.constBegin(); it != words.constEnd(); ++it) { testString += "[" + *it + "] "; } 

However, I don’t understand how I could write it to use QStringBuilder, since I am doing the job here, instead, QStringBuilder requires me to use the% operator and only perform one assignment following the documents.

+8
source share
3 answers

AFAICS here , QStringBuilder does not have a% = operator.

However, if you want to keep your loop, you can try something like this:

 #include <QStringBuilder> #include <QStringList> #include <iostream> using namespace std; int main(int argc, char *argv[]) { QStringList words; words << "a1" << "a2" << "a3"; QString testString; for (auto it = words.constBegin(); it != words.constEnd(); ++it) { testString = testString % *it % " "; } cout << testString.toStdString() << endl; } 

Also mentioned is the QT_USE_QSTRINGBUILDER macro, which turns all + usage into % , provided that this does not cause problems elsewhere in your code.

EDIT:

In light of Marvin's comment, I think I should add a few clarifications to my answer: This answer shows one way to explicitly use QStringBuilder and the% operator in a loop. QStringBuilder was created to optimize concatenation expressions, and this optimization is achieved by eliminating the need for time series and calculating the total size of the concatenated string and distributing it immediately (obviously, this can be done only at the "end" of the expression).

This means that its optimal use is probably not in a loop (for example, the code above). However, even then it gives you some optimization, as can be seen from the gprof and Measure-Command output for the two versions below.

Version 1 - QStringBuilder and% operator (gprof cumulative seconds: 0.46; PowerShell Measure-Command: 5: 23s)

 for (auto it = words.constBegin(); it != words.constEnd(); ++it) { for (int i = 0; i < 100000; ++i) { testString = testString % *it % " "; } } 

Version 2 - Qstring and + operator (gprof cumulative seconds: 0.61; PowerShell Measure-Command: 10: 47s)

 for (auto it = words.constBegin(); it != words.constEnd(); ++it) { for (int i = 0; i < 100000; ++i) { testString = testString + *it + " "; } } 

So, I would say that using QStringBuilder and the% operator probably will not leave you noticeably worse (note that the above values ​​are a little distorted if your application does not actually perform thousands of concatenations without any I / O). But, as usual, it is up to you to measure the lead time and decide which is best for you.

+5
source

I think as long as you have a string

 DEFINES *= QT_USE_QSTRINGBUILDER 

in your .pro file, the compiler will use QStringBuilder to concatenate strings. This will work in Qt 4.8 and higher.

EDIT : Qt docs say that this macro can make your project incompatible with source code. This article talks about how to get around this. Basically, you need to explicitly pass the result of the + expression to a QString , for example. use QString(s1 + s2) instead of s1 + s2 .

+2
source

QString has a reserve() method that allows you to reserve most of the memory in front. This is useful, for example, in loops when the size of the resulting row can be estimated approximately, since it can be used to avoid constant distributions due to constantly growing rows.

0
source

All Articles