简化这个? [英] Simplify this ?

查看:73
本文介绍了简化这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这是一个我编写的简单函数,不会很难理解

它的作用,是的,我知道,你可能会告诉我这是一个糟糕的代码,我想

想知道如何更好地编写它,也许使用stringstream?这是:


std :: string IntToDiskSpace(int size)

{

if(size< 1024)

返回IntToStr(大小)+字节;

否则if(size< 1048576)

返回IntToStr(大小/ 1024)+KB;

else if (大小< 1073741824)

{

char * temp = new char [64];

sprintf(temp,"%2f" ,size / 1048576);

string result = temp;

result + =" MB" ;;

删除临时;

返回结果;

}

其他

{

char * temp = new char [64];

sprintf(temp,"%2f",size / 1073741824);

string result = temp;

result + =" GB" ;;

删除临时;

返回结果;

}

}

解决方案

" Flzw" < FL **** @ wanadoo.fr>在留言中写道

news:41 ********************** @ news.wanadoo.fr ...

好吧,这是一个我编写的简单函数,不太难理解
它的作用,


不,我们不知道它的作用,因为它调用了一个函数

,其定义你不会显示。

和YES,我知道,你可能会告诉我这是一个糟糕的代码,


嗯,这是不完整的代码。它不会编译。如果你想要

的人来评估它,最好发布一些可编辑的内容

如果可能的话。

我会<想知道如何更好地编写它,也许使用stringstream?这是






我建议你用英语告诉我们你想要它做什么。

是的,我会摆脱例如''sprintf()'那里

是更安全,更健壮的生成字符串的方法。


我也会转储不必要的动态分配。


-Mike


好吧,正如迈克指出的那样,没有其他功能的代码我

已经制作了,但这与我的问题无关,但是我同意这是

不一定是显而易见的,所以让我们把代码减少到这个:


std :: string IntToDiskSpace(int size)

{

if(size< 1073741824)

{

char * temp = new char [64];

sprintf(temp,"%2f",size / 1048576);

string result = temp;

result + =" MB" ;;

删除临时;

返回结果;

}

其他

{

char * temp = new char [64];

sprintf(temp,"%2f",size / 1073741824);

string result = temp;

result + =" GB" ;;

删除临时;

返回结果;

}

}

我知道我不应该使用sprintf(因此不必要的动态分配

但是我不确定如何进行dit,我猜stringstream或stringbuf可以帮助

但是这些对我来说非常模糊......


" Flzw"< fl **** @ wanadoo.fr>写道...

好吧,这是一个我编码的简单函数,不会难以理解
它的作用是什么,是的,我知道,你可能会告诉它我这是可怕的代码,


它并不可怕。它只是充满了假设,这些假设不一定是正确的b $ b和b一些未定义的行为。只是一点点未定义

的行为,不是很多。好吧,实际上,不止一点点。

是的,这太糟糕了。

我想知道如何更好地写它,也许使用str ingstream?这里是


std :: string IntToDiskSpace(int size)
{
if(size< 1024)
返回IntToStr(大小)+字节;
否则如果(大小< 1048576)
返回IntToStr(大小/ 1024)+KB;
否则if(size <1073741824)


1073741824是一个不一定适合int的文字。你可能会想要考虑使用''L''后缀。

{
char * temp = new char [64];


你真的不需要动态分配''temp''。只需将

数组声明为自动:


char temp [64];

sprintf(temp,"%2f" ,size / 1048576);


你可能想要打印小数点,你需要的格式是''%。2f''。

其次,''size / 1048576' '的类型为''int'',所以如果你的格式为%f,那么''sprintf''需要一个双精度值。那么,你_need_提供

a那里的双倍值,否则它的行为是不确定的:


sprintf(temp,"%。2f",size / 1048576 。);

string result = temp;
结果+ =" MB" ;;
删除临时;


另一点未定义的行为。如果你使用''new []''进行分配,你需要使用''删除[]''来释放它:


delete [] temp;


当然,如果你按照我的建议使用一个简单的自动阵列,你就不需要删除
了。

返回结果;
}

{


此块中的相同注释如上所述。

char * temp = new char [64];
sprintf(temp,"%2f",size / 1073741824);
string result = temp;
result + =" GB"
删除临时;
返回结果;
}
}




简化版可能会像这样开始


std :: ostringstream os;

if(size< 1024)

return(os<< size< ;<" Bytes")。s​​tr();

size / = 1024;

if(size< 1024)

return (os<<<<<"">)。s​​tr();

...


Victor


Alright, here is a simple function I coded, won''t be hard understanding what
it does, and YES, I know, you will probably tell me it''s awful code, I would
like to know how to write it better, maybe using stringstream ? here it is :

std::string IntToDiskSpace( int size)
{
if (size < 1024)
return IntToStr( size ) + " Bytes";
else if (size < 1048576)
return IntToStr( size / 1024) + "KB";
else if (size < 1073741824)
{
char* temp = new char[64];
sprintf( temp, "%2f", size / 1048576);
string result = temp;
result += " MB";
delete temp;
return result;
}
else
{
char* temp = new char[64];
sprintf( temp, "%2f", size / 1073741824);
string result = temp;
result += " GB";
delete temp;
return result;
}
}

解决方案

"Flzw" <fl****@wanadoo.fr> wrote in message
news:41**********************@news.wanadoo.fr...

Alright, here is a simple function I coded, won''t be hard understanding what it does,
No, we cannot know what it does, since it calls a function
whose definition you don''t show.
and YES, I know, you will probably tell me it''s awful code,
Well, it''s incomplete code. It won''t compile. If you want
folks to assess it, it''s much better to post something compilable
if at all possible.
I would
like to know how to write it better, maybe using stringstream ? here it is


:

I suggest you tell us, in English, what you want it to do.
And yes, I''d get rid of the e.g. ''sprintf()''s as there
are safer, more robust ways to generate strings.

I''d also dump the unnecessary dynamic allocations.

-Mike


Allright, as Mike pointed out, there was not the code for another function I
made but that was irrelevant in the questions I had but yes I agree that was
not necessarily obvious, so let''s reduce the code to this :

std::string IntToDiskSpace( int size)
{
if (size < 1073741824)
{
char* temp = new char[64];
sprintf( temp, "%2f", size / 1048576);
string result = temp;
result += " MB";
delete temp;
return result;
}
else
{
char* temp = new char[64];
sprintf( temp, "%2f", size / 1073741824);
string result = temp;
result += " GB";
delete temp;
return result;
}
}

I know I shouldn''t use sprintf (thus the "unecessary dynamic allocations"
but I''m unsure on how to dit, I guess stringstream or stringbuf could help
there but these are quite obscure for me...


"Flzw" <fl****@wanadoo.fr> wrote...

Alright, here is a simple function I coded, won''t be hard understanding
what
it does, and YES, I know, you will probably tell me it''s awful code,
It''s not awful. It''s just full of assumptions which are not necessarily
correct, and some undefined behaviour. Just a little bit of undefined
behaviour, not a whole lot. Well, actually, more than a little bit.
Yeah, it''s awful.
I would
like to know how to write it better, maybe using stringstream ? here it is
:

std::string IntToDiskSpace( int size)
{
if (size < 1024)
return IntToStr( size ) + " Bytes";
else if (size < 1048576)
return IntToStr( size / 1024) + "KB";
else if (size < 1073741824)
1073741824 is a literal that doesn''t necessarily fit into an int. You might
want to consider using ''L'' suffix.
{
char* temp = new char[64];
You really don''t need to dynamically allocate ''temp''. Just declare the
array as automatic:

char temp[64];
sprintf( temp, "%2f", size / 1048576);
You probably wanted to print decimal points, the format you need is ''%.2f''.
Second, the ''size / 1048576'' has the type ''int'', so if you have %f for
the format, the ''sprintf'' expects a double value. So, you _need_ to supply
a double value there, otherwise its behaviour is undefined:

sprintf(temp, "%.2f", size / 1048576.);
string result = temp;
result += " MB";
delete temp;
Another bit of undefined behaviour. If you allocate using ''new[]'', you
MUST free it using ''delete[]'':

delete[] temp;

Of course, if you follow my advice to use a simple automatic array, you
don''t need to delete at all.
return result;
}
else
{
Same notes in this block as above.
char* temp = new char[64];
sprintf( temp, "%2f", size / 1073741824);
string result = temp;
result += " GB";
delete temp;
return result;
}
}



A simplified version might begin like this

std::ostringstream os;
if (size < 1024)
return (os << size << " Bytes").str();
size /= 1024;
if (size < 1024)
return (os << size << " KB").str();
...

Victor


这篇关于简化这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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