使用stringstream进行类型转换的奇怪问题 [英] Strange problem of typecasting with stringstream

查看:66
本文介绍了使用stringstream进行类型转换的奇怪问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用stringstream将一些字符串转换成整数,然后

这个奇怪的问题加速了。

这是我的测试代码:


stringstream conv;

string from;

int to;


from =" 1" ;;

conv<<来自;

conv> to;


from =" 2";

conv<<来自;

conv> to;


第一次演出工作正常,但第二次演出没有奏效

all。

conv.str()返回1在''from =" 2"之后; conv<<来自;''已执行。

然后我明确更改了conv的缓冲区,如下所示:


conv.str(" 1");

conv> to;


conv.str(" 2");

conv> to;


尽管如此,第一次演出工作完美,第二次演出失败了,= =

1.

我想知道我'我非常误解了一些事情,对不起,如果我是

问明显的话。

I was trying to cast some strings to integers with stringstream, then
this strange problem poped up.
Here is my test code:

stringstream conv;
string from;
int to;

from = "1";
conv << from;
conv >to;

from = "2";
conv << from;
conv >to;

The first casting worked fine, but the second casting didn''t work at
all.
conv.str() returned "1" after ''from = "2"; conv << from;'' was executed.
Then I explicitly changed the buffer of conv, like this:

conv.str("1");
conv >to;

conv.str("2");
conv >to;

Still, the first casting worked perfectly, the second one failed, to ==
1.
I''m wondering I''ve terribly misunderstanding something, sorry if I''m
asking the obvious.

推荐答案

< ; vi ****** @ gmail.com写信息

新闻:11 ********************* @ c28g2000cwb.googlegro ups.com ...
<vi******@gmail.comwrote in message
news:11*********************@c28g2000cwb.googlegro ups.com...

>我试图用stringstream将一些字符串转换成整数,然后

这个奇怪的问题加速了。

这是我的测试代码:


stringstream conv;

string from;

int to;


from =" 1";

con v<<来自;

conv> to;


from =" 2";

conv<<来自;

conv> to;


第一次演出工作正常,但第二次演出没有奏效

all。

conv.str()返回1在''from =" 2"之后; conv<<来自;''已执行。

然后我明确更改了conv的缓冲区,如下所示:


conv.str(" 1");

conv> to;


conv.str(" 2");

conv> to;


尽管如此,第一次演出工作完美,第二次演出失败了,= =

1.

我想知道我'非常误解了一些事情,对不起,如果我是

问明显的事情。
>I was trying to cast some strings to integers with stringstream, then
this strange problem poped up.
Here is my test code:

stringstream conv;
string from;
int to;

from = "1";
conv << from;
conv >to;

from = "2";
conv << from;
conv >to;

The first casting worked fine, but the second casting didn''t work at
all.
conv.str() returned "1" after ''from = "2"; conv << from;'' was executed.
Then I explicitly changed the buffer of conv, like this:

conv.str("1");
conv >to;

conv.str("2");
conv >to;

Still, the first casting worked perfectly, the second one failed, to ==
1.
I''m wondering I''ve terribly misunderstanding something, sorry if I''m
asking the obvious.



我怀疑字符串流是否具有12。而不是1 2而不是1 2。或者

soemthing。我会尝试以下直到我发现什么有效。


from =" 1";

conv<< from;

conv> to;


from =" 2" ;;

conv<<来自;

conv> to;


如果没有工作,请查看


from =" ; 1 2" ;;

conv<<来自;

conv> to;

conv> to;


给你。

你可能需要在两者之间重置字符串流,但我从来没有那个

问题。

I"m suspecting that the stringstream is having "12" instead of "1 2" or
soemthing. I would try the following til I found what worked.

from = "1";
conv << from;
conv >to;

from = " 2";
conv << from;
conv >to;

if that didnt'' work see what

from = "1 2";
conv << from;
conv >to;
conv >to;

gives you.

You may need to reset the stringstream in between, but I''ve never had that
problem.


vi ****** @ gmail.com 写道:

我试图用stringstream将一些字符串转换为整数,然后

这个奇怪的问题加速了。

这是我的测试代码:


stringstream conv;

string from;

int to;


from =" 1";

conv<<来自;

conv> to;
I was trying to cast some strings to integers with stringstream, then
this strange problem poped up.
Here is my test code:

stringstream conv;
string from;
int to;

from = "1";
conv << from;
conv >to;



尝试:

conv.clear();

Try:
conv.clear();


>

from =" 2";

conv<<来自;

conv> to;
>
from = "2";
conv << from;
conv >to;



问候,

Sumit。

Regards,
Sumit.




Jim Langston写道:

Jim Langston wrote:

from =" 1";

conv<< from;

conv> to;


from =" 2" ;;

conv<<来自;

conv> to;
from = "1";
conv << from;
conv >to;

from = " 2";
conv << from;
conv >to;



无效。

from =" 2" ;;

conv<< from;

没有改变conv的内部缓冲区,conv.str()仍然返回1。

didn''t work.
from = " 2";
conv << from;
didn''t change the inner buffer of conv, conv.str() still returned "1".


if没有工作,看看是什么


from =" 1 2";

conv<<来自;

conv> to;

conv> to;
if that didnt'' work see what

from = "1 2";
conv << from;
conv >to;
conv >to;



这是有用的。

this worked.


给你。


您可能需要在两者之间重置字符串流,但我从未有过这个

问题。
gives you.

You may need to reset the stringstream in between, but I''ve never had that
problem.


这篇关于使用stringstream进行类型转换的奇怪问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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