为什么输出str2不是hello world 呢

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

img

遇到空格(空字符)读取中断。
因为空格、回车作为默认的分隔符。
相当于缓存流中有两个缓存数据,in>>str2只读取了其中一个,还有一个留在缓存流中。
你可以再读取一次,得到的就是world了。

stringstream遇到空格就认为一个字符串结束了,所以in现在里面还剩个world,你再用个str3去接收打印出来就是world

你用的就是isstringstream这个类,没关注这个类的具体细节吗? 默认回车或者换行来分割字符串了
可以百度了解一下这个类及其用法。
所以你这里只是一次写入str2,只是把第一个切割的字符串写入

img

https://en.cppreference.com/w/cpp/string/basic_string/operator_ltltgtgt

  1. Behaves as a FormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, first clears str with str.erase(), then reads characters from is and appends them to str as if by str.append(1, c), until one of the following conditions becomes true:
  • N characters are read, where N is is.width() if is.width() > 0, otherwise N is str.max_size()
  • the end-of-file condition occurs in the stream is
  • std::isspace(c,is.getloc()) is true for the next character c in is (this whitespace character remains in the input stream).

因为空格隔断了,就比如说话说到一半被打断了一样
希望对题主有所帮助,可以的话,帮忙点个采纳!

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632

因为赋值的时候遇到空格(空字符,换行符也是如此)停止了,中断读取,只留下了空格前面的hello

img