编写代码的程序:它应该使用'string'吗? [英] A program that writes code: should it use 'string'?

查看:74
本文介绍了编写代码的程序:它应该使用'string'吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在编写一个生成源代码的程序。看下面的一个片段

。我的问题是关于使用那个不断增长的代码变量。

它有效吗?这个案例是推荐的吗?


生成的代码可以增长很多。也许我应该提前分配一个大的

最大尺寸?


TIA,


-RFH
-------------


void SynthesizeTextField(CompleteField fullTextField)

{

字符串代码;

string baseFieldname =" text" ;;

stringstream ss;

static int subindex = 1;


code =" Field";

code + = baseFieldname;

ss<< subindex;

code + = ss.str();

code + =" " ;;

code + =" doc.FieldCreate(\"" ;;

code + = baseFieldname;

code + = ss.str();

code + =" \",Field :: e_text,\" \",\" \");" ;;


subindex ++;

}

解决方案

Ramon F Herrera写道:


>

我正在编写一个生成源代码的程序。看下面的一个片段

。我的问题是关于使用那个不断增长的代码变量。

它有效吗?这种情况是推荐的吗?



建议在优化之前进行测量。编写程序,以便它很容易理解。
。当(并且仅当)你遇到性能问题时,

不要猜测原因可能是什么;相反,使用分析器来识别瓶颈然后做一些事情。


生成的代码可以增长许多。也许我应该提前分配一个大的

最大尺寸?



[snip]


如果分析显示附加到字符串太昂贵,请保留

某些容量将是第一个尝试的东西。它是最不具侵入性的

衡量。

最好


Kai-Uwe Bux


Ramon F Herrera< ra *** @ conexus.netwrote:


我正在编写一个生成源代码的程序。看下面的一个片段

。我的问题是关于使用那个不断增长的代码变量。

它有效吗?这个案例是推荐的吗?


生成的代码可以增长很多。也许我应该提前分配一个大的

最大尺寸?



std :: string只是一个向量< char,带有一些额外的函数,你可能不需要这个特定的变量



我认为你最好的选择是让你自己的班级代表

代码,如果有意义的话,用字符串实现该类对你来说。

好​​的一点是你可以随时更改类的实现

,因为分析需要,而不会影响任何其他代码。


6月1日晚上11:58,Ramon F Herrera< ra ... @ conexus.netwrote:


我正在写作一个生成源代码的程序。看下面的一个片段

。我的问题是关于使用那个不断增长的代码变量。

它有效吗?这种情况是推荐的吗?


生成的代码可以增长很多。也许我应该提前分配一个大的

最大尺寸?


-------------


void SynthesizeTextField(CompleteField fullTextField)

{

字符串代码;

string baseFieldname =" text" ;;

stringstream ss;

static int subindex = 1;


code =" Field";

code + = baseFieldname;

ss<< subindex;

code + = ss.str();

code + =" " ;;

code + =" doc.FieldCreate(\"" ;;

code + = baseFieldname;

code + = ss.str();

code + =" \",Field :: e_text,\" \",\" \");" ;;


subindex ++;

}



对于初学者,我会生成(或支持生成)直接输入输出流的
。类似于:


std :: ostream&

SynthesizeTextField(

std :: ostream& dest,

...)

{

// ...

返回dest;

}


你在这里格式化(有些数据是数字的,显然是b $ b),所以你不妨将整个事情视为

stream。你肯定会在最后输出它;

你可以用

程序中的C ++源代码做多少,所以你可能会这样做很好地直接生成输出

流,并且永远不会构建字符串。


-

James Kanze(GABI软件)电子邮件:ja ********* @ gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''coco,法国,+ 33(0)1 30 23 00 34



I am writing a program that generates source code. See a snippet
below. My question is about the use of that growing ''code'' variable.
Is it efficient? Is is recommended for this case?

The code generated can grow a lot. Perhaps I should allocate a large
max size in advance?

TIA,

-RFH
-------------

void SynthesizeTextField(CompleteField fullTextField)
{
string code;
string baseFieldname = "text";
stringstream ss;
static int subindex = 1;

code = "Field ";
code += baseFieldname;
ss << subindex;
code += ss.str();
code += " ";
code += "doc.FieldCreate(\"";
code += baseFieldname;
code += ss.str();
code += "\", Field::e_text, \"\", \"\");";

subindex++;
}

解决方案

Ramon F Herrera wrote:

>
I am writing a program that generates source code. See a snippet
below. My question is about the use of that growing ''code'' variable.
Is it efficient? Is is recommended for this case?


Recommended is to measure before you optimize. Write the program so that it
is easy to understand. When (and only when) you have a performance problem,
don''t guess what the cause might be; instead, use a profiler to identify
the bottleneck and then do something about it.

The code generated can grow a lot. Perhaps I should allocate a large
max size in advance?

[snip]

If profiling shows that appending to the string is too costly, reserving a
certain capacity would be the first thing to try. It''s the least intrusive
measure.
Best

Kai-Uwe Bux


Ramon F Herrera <ra***@conexus.netwrote:

I am writing a program that generates source code. See a snippet
below. My question is about the use of that growing ''code'' variable.
Is it efficient? Is is recommended for this case?

The code generated can grow a lot. Perhaps I should allocate a large
max size in advance?

A std::string is just a vector<charwith some extra functions that you
probably don''t need for this particular variable.

I think your best bet would be to make your own class to represent
"code", implement that class with a string if that makes sense to you.
The nice thing is you can always change the implementation of the class
later as profiling requires, without affecting any other code.


On Jun 1, 11:58 pm, Ramon F Herrera <ra...@conexus.netwrote:

I am writing a program that generates source code. See a snippet
below. My question is about the use of that growing ''code'' variable.
Is it efficient? Is is recommended for this case?

The code generated can grow a lot. Perhaps I should allocate a large
max size in advance?

-------------

void SynthesizeTextField(CompleteField fullTextField)
{
string code;
string baseFieldname = "text";
stringstream ss;
static int subindex = 1;

code = "Field ";
code += baseFieldname;
ss << subindex;
code += ss.str();
code += " ";
code += "doc.FieldCreate(\"";
code += baseFieldname;
code += ss.str();
code += "\", Field::e_text, \"\", \"\");";

subindex++;
}

For starters, I''d generate (or support generation) directly into
the output stream. Something like:

std::ostream&
SynthesizeTextField(
std::ostream& dest,
... )
{
// ...
return dest ;
}

You''re formatting here (some of the data is numeric,
apparently), so you might as well treat the entire thing as a
stream. And you''ll certainly be outputting it in the end;
there''s not much you can do with C++ source code within the
program, so you might as well generate directly into the output
stream, and never build the string at all.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


这篇关于编写代码的程序:它应该使用'string'吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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