Strange Qt Code with strings

為{幸葍}努か 提交于 2019-12-06 21:21:42

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!