一个问题:将double转换为字符串 [英] A question: Convert double to string

查看:67
本文介绍了一个问题:将double转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我需要这么快(安全地转换成双字符串)快速,安全且便宜的b $ b。有没有办法做到这一点?


除了以下方式:

1. C ++ I / O流,stringstream(和boost :: lexical_cast)

2. sprintf(及其兄弟)

3.任何非标准的C / C ++函数


PS:如果有任何i的实现/ o流比

sprintf快,请告诉我,谢谢。

hi

I need to do this (convert double to string) fast, safe and
portable. Is there any way to do this ?

Except the ways following:
1. C++ I/O stream, stringstream (and boost::lexical_cast)
2. sprintf (and its brothers)
3. any nonstandard C/C++ functions

PS: if any implementation of i/o stream was faster than
sprintf, please tell me, thank you.

推荐答案

Aman JIANG写道:
Aman JIANG wrote:

hi


我需要快速,安全地执行此操作(将double转换为字符串)

便携式。有没有办法做到这一点?


除了以下方式:

1. C ++ I / O流,stringstream(和boost :: lexical_cast)

2. sprintf(及其兄弟)

3.任何非标准C / C ++函数
hi

I need to do this (convert double to string) fast, safe and
portable. Is there any way to do this ?

Except the ways following:
1. C++ I/O stream, stringstream (and boost::lexical_cast)
2. sprintf (and its brothers)
3. any nonstandard C/C++ functions



怎么样应该可能吗?你不想要任何标准的方式来做这件事你不想要任何非标准的做法。我想

会限制选择。

How is that supposed to be possible? You don''t want any standard way of
doing it and you don''t want any non-standard way of doing it. I guess that
limits the choices a bit.


9月22日下午3:11,Rolf Magnus< ramag ... @ t-online.dewrote:
On Sep 22, 3:11 pm, Rolf Magnus <ramag...@t-online.dewrote:

Aman JIANG写道:
Aman JIANG wrote:

hi
hi


我需要快速,安全地执行此操作(将double转换为字符串)并且b $ b b b portable。有没有办法做到这一点?
I need to do this (convert double to string) fast, safe and
portable. Is there any way to do this ?


以下方式除外:

1. C ++ I / O流,stringstream(和boost :: lexical_cast)

2. sprintf(及其兄弟)

3.任何非标准C / C ++函数
Except the ways following:
1. C++ I/O stream, stringstream (and boost::lexical_cast)
2. sprintf (and its brothers)
3. any nonstandard C/C++ functions



怎么样应该可能吗?你不想要任何标准的方式来做这件事你不想要任何非标准的做法。我想

限制了一些选择。


How is that supposed to be possible? You don''t want any standard way of
doing it and you don''t want any non-standard way of doing it. I guess that
limits the choices a bit.



谢谢。


我想这是可能的,因为我在问一个问题。我有这个

问题因为:

A. sprintf难以安全

B. iostream很慢,很难相信它可以用在一个有很多很多操作的

程序中,它有许多不必要的

动作
仅用于数值转换。


所以,我想找到一个更好的方法。

(抱歉我的英语不好)

Thanks.

I suppose it is possible because i am asking a question. And I have
this
question because:
A. sprintf was hard to be safe
B. iostream was pretty slow, it''s hard to believe it can be used on a
program what have lots and lots of operation, It has many unwanted
actions
just for a numerical value conversion.

So, i want to find a better way.
(sorry for my poor english)


" Aman JIANG" < Am ******* @ gmail.comwrote in message

news:11 ********************** @ k79g2000hse.googlegr oups.com ...

:9月22日下午3:11,Rolf Magnus< ramag ... @ t-online.dewrote:

:Aman JIANG写道:

:嗨

:>

:我需要快速,安全地执行此操作(将double转换为字符串)

:便携式。有没有办法做到这一点?

:>

:以下方式除外:

:1。C ++ I / O流, stringstream(和boost :: lexical_cast)

:2。sprintf(及其兄弟)

:3。任何非标准的C / C ++函数

:>

:这怎么可能?你不需要任何标准的

:这样做的方式,你不想要任何非标准的做法

:它。我想这会限制选择。



:谢谢。



:我想它是可能的,因为我问了一个问题。

:我有这个问题因为:

:A。sprintf很难安全

它是。 sprintf的更安全的变体可以提供帮助,因为可以将

直接写入文件。但最终,检查转换数字的范围和有效性可能有意义




:B。iostream非常慢,它是'很难相信它可以被使用

:在一个程序上有很多很多操作,它有很多

:不需要的动作只是为了进行数值转换。 />
字符串流方法有它的开销,但是很安全。


:所以,我想找到一个更好的方法。

:(对不起我糟糕的英语)

如果iostream对你的应用程序来说你的平台太慢了,那么

对性能敏感的方法就是printf系列函数。

因为你可能想要使用一些特定于平台的扩展

来保证它的安全,所以将它包装好后可能是个好主意

你自己的功能 - 使用你想要的缓冲区分配和格式 -

规范策略。

为了重写或修改dtoa功能到你的应用程序中,

我怀疑它在性能方面是否是一个合理的优先级

优化(应该有更重要的事情要调整......)


hth -Ivan

-
http://ivan.vecerina.com/contact/?subject=NG_POST < - 电子邮件联系表格

Brainbench MVP for C ++< http://www.brainbench.com

"Aman JIANG" <Am*******@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
: On Sep 22, 3:11 pm, Rolf Magnus <ramag...@t-online.dewrote:
: Aman JIANG wrote:
: hi
: >
: I need to do this (convert double to string) fast, safe and
: portable. Is there any way to do this ?
: >
: Except the ways following:
: 1. C++ I/O stream, stringstream (and boost::lexical_cast)
: 2. sprintf (and its brothers)
: 3. any nonstandard C/C++ functions
: >
: How is that supposed to be possible? You don''t want any standard
: way of doing it and you don''t want any non-standard way of doing
: it. I guess that limits the choices a bit.
:
: Thanks.
:
: I suppose it is possible because i am asking a question.
: And I have this question because:
: A. sprintf was hard to be safe
It is. Safer variants of sprintf can help, as can writing
directly to a file. But ultimately, it might make sense
to check the range and validity of the converted number.

: B. iostream was pretty slow, it''s hard to believe it can be used
: on a program what have lots and lots of operation, It has many
: unwanted actions just for a numerical value conversion.
The stringstream approach has its overheads, agreed, but is safe.

: So, i want to find a better way.
: (sorry for my poor english)
If iostream is too slow on your platform for your application, the
performance-sensitive way to go is the printf family of functions.
Because you might want to use some platform-specific extension
to make it safe, it could be a good idea to wrap it behind
your own function - using the buffer-allocation and format-
specification policy you want.
As to rewriting or adapt a dtoa function into your application,
I doubt it would be a sensible priority in terms of performance
optimization (there should be more important things to tune...)

hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com


这篇关于一个问题:将double转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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