In C ++ 11, std::basic_string supports move semantics, which means that you can optimize the concatenation of a number of lines with operator+ by allocating memory for the first line in the series, and then just building the remaining lines in the memory of the first line in the series, significantly reducing the number of memory allocations and copies needed to concatenate and return a series of lines.
I'm sure there are further optimizations that you can perform, as you pointed out using the Qt method, but the semantics of move allowed by C ++ 11 overcome the huge performance barriers that existed in the C ++ 03 version of std::basic_string , especially when combining a large number of lines together.
So for example, something like
std::string a = std::string("Blah blah") + " Blah Blah " + " Yadda, Yadda";
can be done by allocating memory for the first line, and then using the semantics of movement, to "steal" the remaining memory from the first line to build the second or two lines in place, and only reallocate the memory at startup from extra space. Finally, the assignment operator can, using move-semantics, βstealβ the memory from the temporary value r created on the right side of the assignment operator, preventing a copy of the concatenated string.
Jason source share