遇到空格(空字符)读取中断。
因为空格、回车作为默认的分隔符。
相当于缓存流中有两个缓存数据,in>>str2只读取了其中一个,还有一个留在缓存流中。
你可以再读取一次,得到的就是world了。
stringstream遇到空格就认为一个字符串结束了,所以in现在里面还剩个world,你再用个str3去接收打印出来就是world
你用的就是isstringstream这个类,没关注这个类的具体细节吗? 默认回车或者换行来分割字符串了
可以百度了解一下这个类及其用法。
所以你这里只是一次写入str2,只是把第一个切割的字符串写入
https://en.cppreference.com/w/cpp/string/basic_string/operator_ltltgtgt
- 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).
因为空格隔断了,就比如说话说到一半被打断了一样
希望对题主有所帮助,可以的话,帮忙点个采纳!
因为赋值的时候遇到空格(空字符,换行符也是如此)停止了,中断读取,只留下了空格前面的hello