问题
I found my friend's Qt code and he uses the modulo operator on two QStrings like this:
QString result = oneString % twoString;
What does it mean?
回答1:
It's just another (more efficient) way to concatenate QStrings as described in the manual
QStringBuilder uses expression templates and reimplements the '%' operator so that when you use '%' for string concatenation instead of '+', multiple substring concatenations will be postponed until the final result is about to be assigned to a QString. At this point, the amount of memory required for the final result is known. The memory allocator is then called once to get the required space, and the substrings are copied into it one by one.
回答2:
It is Qt specific way of string construction. Take a look on this page.
QStringBuilder uses expression templates and reimplements the '%' operator so that when you use '%' for string concatenation instead of '+', multiple substring concatenations will be postponed until the final result is about to be assigned to a QString. At this point, the amount of memory required for the final result is known. The memory allocator is then called once to get the required space, and the substrings are copied into it one by one.
来源:https://stackoverflow.com/questions/12027977/strange-qt-code-with-strings