奇怪的Qt代码与字符串 [英] Strange Qt Code with strings

查看:181
本文介绍了奇怪的Qt代码与字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了我的朋友的Qt代码,他对两个 QString 使用模运算符,如下所示:

I found my friend's Qt code and he uses the modulo operator on two QStrings like this:

QString result = oneString % twoString;

这是什么意思?

推荐答案

这是另一种(更高效的)连接 QString 的方法,如手册

It's just another (more efficient) way to concatenate QStrings as described in the manual


QStringBuilder使用表达式模板并重新实现'% '
运算符,以便当您使用'%'字符串连接而不是
'+'时,多个子字符串连接将推迟,直到
最终结果即将分配给QString。在这一点上,最终结果所需的
内存量是已知的。然后调用内存
分配器一次以获得所需的空间,并将
子字符串逐个复制到其中。

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.

这篇关于奇怪的Qt代码与字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆