QStringBuilder improves the efficiency of QString connection

QString A = "B" + "C" + "D";

When connecting strings to this way, multiple temporary objects will be created, which means that memory space must be applied for/released multiple times.

The Qt help document describes how to use QStringBuilder to improve connection efficiency:

      #include <QStringBuilder>

      QString hello("hello");
      QStringRef el(&hello, 2, 3);
      QString world("world");
      QString message =  hello % el % world % QChar('!');

The connection of multiple substrings will be delayed until the final result will be assigned to a QString. To connect, the memory required for the final result is known. Then call the memory allocator once to obtain the required space, and copy the substrings into it one by one.

If you add in the pro file:

      DEFINES *= QT_USE_QSTRINGBUILDER

"+" will be automatically executed as "%" of QStringBuilder.

Note: I personally think that as long as it is not very frequent splicing for a long time, it will not cause a performance bottleneck.

Use C++17 fold expression to realize efficient QString splicing

Use QStringBuilder for string concatenation

Guess you like

Origin blog.csdn.net/kenfan1647/article/details/113829123