for循环中的Qt字符串生成器 [英] Qt string builder in for loop

查看:63
本文介绍了for循环中的Qt字符串生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循thisthis 文档我会在一个for 循环.我应该应用它的代码是

following this and this documentation I would use the QStringBuilder in a for loop. The code where I should apply it is

QStringList words;
QString testString;

for (auto it = words.constBegin(); it != words.constEnd(); ++it)
{
    testString += "[" + *it + "] ";
}

但是,我不明白如何编写它以使用 QStringBuilder,因为这里我正在做一个赋值,而是 QStringBuilder 要求我使用 % 运算符并按照文档只做一个赋值.

However I don't understand how I could write it to use the QStringBuilder as here I'm doing an assignment, instead the QStringBuilder requires me to use the % operator and do only one assignment following the docs.

推荐答案

AFAICS 这里, QStringBuilder 没有运算符 %=.

AFAICS here, QStringBuilder doesn't have an operator %=.

但是,如果您想保持循环,可以尝试以下操作:

However, if you want to maintain your loop, you could try something like this:

#include <QStringBuilder>
#include <QStringList>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    QStringList words;
    words << "a1" << "a2" << "a3";
    QString testString;

    for (auto it = words.constBegin(); it != words.constEnd(); ++it)
    {
        testString = testString % *it % " ";
    }
    cout << testString.toStdString() << endl;
}

还提到了 QT_USE_QSTRINGBUILDER 宏,它将所有 + 用法转换为 %,前提是它不会在您的其他地方造成问题代码.

There's also mention of the QT_USE_QSTRINGBUILDER macro, which turns all + usage into %, provided that doesn't create problems elsewhere on your code.

鉴于 Marvin 的评论,我认为我应该在我的回答中添加一些说明:此答案显示了在循环中显式使用 QStringBuilder 和 operator% 的一种方法.QStringBuilder 是为了优化串联表达式而创建的,该优化是通过消除对临时变量的需要,并计算串联字符串的总大小并一次分配所有来实现的(显然,这只能在结束"时完成)表达式).

In light of Marvin's comment, I believe I should add a few clarifications to my answer: This answer shows one way of explicitly using QStringBuilder and operator% in a loop. QStringBuilder was created to optimize concatenation expressions, and that optimization is achieved by eliminating the need for temporaries, and calculating the total size of the concatenated string and allocating it all at once (obviously, this can only be done at the "end" of the expression).

这意味着它的最佳使用可能不在循环中(例如上面的代码).但是,即便如此,它也为您提供了某种优化,从下面两个版本的 gprof 和 Measure-Command 输出中都可以看出.

This means its optimal use is probably not in a loop (such as the code above). However, even then it gives you some sort of optimization, as can be seen both from the gprof and Measure-Command output for the two versions below.

版本 1 - QStringBuilder 和 operator%(gprof 累积秒数:0.46;PowerShell Measure-Command:5:23s)

Version 1 - QStringBuilder and operator% (gprof cumulative seconds: 0.46; PowerShell Measure-Command: 5:23s)

for (auto it = words.constBegin(); it != words.constEnd(); ++it)
{
    for (int i = 0; i < 100000; ++i)
    {
        testString = testString % *it % " ";
    }
}

版本 2 - Qstring 和 operator+(gprof 累积秒数:0.61;PowerShell Measure-Command:10:47s)

Version 2 - Qstring and operator+ (gprof cumulative seconds: 0.61; PowerShell Measure-Command: 10:47s)

for (auto it = words.constBegin(); it != words.constEnd(); ++it)
{
    for (int i = 0; i < 100000; ++i)
    {
        testString = testString + *it + " ";
    }
}

所以,我想说使用 QStringBuilder 和 operator% 可能不会让你明显更糟(请注意,上述值有点偏斜,除非你的应用程序实际上在没有任何 I/O 的情况下执行数千个连接).但是,像往常一样,由您来衡量执行时间并决定什么最适合您.

So, I'd say that using QStringBuilder and operator% probably won't leave you noticeably worse (please note that the above values are a bit skewed, unless your app is actually performing thousands of concatenations with no I/O whatsoever). But, as usual, it's up to you to measure your execution times and decide what's working best for you.

这篇关于for循环中的Qt字符串生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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