Redian新闻
>
问一段C++ iostringstream的代码
avatar
问一段C++ iostringstream的代码# Programming - 葵花宝典
e*s
1
using namespace std;
11 string sentence = "And I feel fine...";
12 std::string result;
13 std::istringstream iss (sentence);
14 std::ostringstream oss;
15 string piece;
16 while(iss){
17 iss>>piece;
18 oss<19 piece.clear();
20 }
21 result = oss.str();
22 cout<< result;
其中如果不加19行 输出就是AndIfeelfine...fine...
最后一段被重复了一次 iss究竟如何判断为空呢
有没有其他更好的办法
avatar
k*g
2

http://www.cplusplus.com/reference/string/string/operator%3E%3E
http://www.cplusplus.com/reference/istream/istream/
http://www.cplusplus.com/reference/ios/ios/good/
Usually, istream::good() is used to check the stream's state, because
istream actually has three other non-good states: eof(), bad(), and fail().
The boolean cast is usually not good enough.
Your test code shows that it is necessary to clear the string prior to
reading. So, Line 19 is necessary anyway.
A simpler alternative is to declare a new std::string inside the loop. Then,
every loop iteration the string is guaranteed to be empty.

【在 e*******s 的大作中提到】
: using namespace std;
: 11 string sentence = "And I feel fine...";
: 12 std::string result;
: 13 std::istringstream iss (sentence);
: 14 std::ostringstream oss;
: 15 string piece;
: 16 while(iss){
: 17 iss>>piece;
: 18 oss<: 19 piece.clear();

avatar
t*t
3
这个定式不是这样的, 应该这样写:
while (iss>>piece) {
oss<}

【在 e*******s 的大作中提到】
: using namespace std;
: 11 string sentence = "And I feel fine...";
: 12 std::string result;
: 13 std::istringstream iss (sentence);
: 14 std::ostringstream oss;
: 15 string piece;
: 16 while(iss){
: 17 iss>>piece;
: 18 oss<: 19 piece.clear();

avatar
e*s
4
great! many thanks!

.
Then,

【在 k**********g 的大作中提到】
:
: http://www.cplusplus.com/reference/string/string/operator%3E%3E
: http://www.cplusplus.com/reference/istream/istream/
: http://www.cplusplus.com/reference/ios/ios/good/
: Usually, istream::good() is used to check the stream's state, because
: istream actually has three other non-good states: eof(), bad(), and fail().
: The boolean cast is usually not good enough.
: Your test code shows that it is necessary to clear the string prior to
: reading. So, Line 19 is necessary anyway.
: A simpler alternative is to declare a new std::string inside the loop. Then,

avatar
e*s
5
many thanks!

【在 t****t 的大作中提到】
: 这个定式不是这样的, 应该这样写:
: while (iss>>piece) {
: oss<: }

avatar
e*s
6
谢谢
请问一下和
while(iss){
iss>>piece;
oss<}
的问题在哪里呢 iss>>piece 返回的也应该是&iss, 在末尾判定为false, 上面那段里
面是在下个iteration判断iss 但是似乎判定还是true

【在 t****t 的大作中提到】
: 这个定式不是这样的, 应该这样写:
: while (iss>>piece) {
: oss<: }

avatar
e*s
7
yes use good works!
thanks!

.
Then,

【在 k**********g 的大作中提到】
:
: http://www.cplusplus.com/reference/string/string/operator%3E%3E
: http://www.cplusplus.com/reference/istream/istream/
: http://www.cplusplus.com/reference/ios/ios/good/
: Usually, istream::good() is used to check the stream's state, because
: istream actually has three other non-good states: eof(), bad(), and fail().
: The boolean cast is usually not good enough.
: Your test code shows that it is necessary to clear the string prior to
: reading. So, Line 19 is necessary anyway.
: A simpler alternative is to declare a new std::string inside the loop. Then,

avatar
k*g
8

是operation 前和後的分别。
Thrust 的答案也是很重要的。建议再多做几个测试。
In your code, the stream's state is tested first and then the string is read
. in Thrust's code, the string is read first, and then the stream's state is
tested. If the test is bad, the string is thrown away.
You should do a few more tests, e.g. a string that ends with a whitespace (
the delimiter), a string that contains a single word (no whitespace at all),
etc., to make sure the code satisfies all of your needs.

【在 e*******s 的大作中提到】
: yes use good works!
: thanks!
:
: .
: Then,

avatar
e*s
9
I guess I understand what's happening.
Thanks a lot!

read
is
),

【在 k**********g 的大作中提到】
:
: 是operation 前和後的分别。
: Thrust 的答案也是很重要的。建议再多做几个测试。
: In your code, the stream's state is tested first and then the string is read
: . in Thrust's code, the string is read first, and then the stream's state is
: tested. If the test is bad, the string is thrown away.
: You should do a few more tests, e.g. a string that ends with a whitespace (
: the delimiter), a string that contains a single word (no whitespace at all),
: etc., to make sure the code satisfies all of your needs.

avatar
m*l
10

.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~?????
Then,

【在 k**********g 的大作中提到】
:
: 是operation 前和後的分别。
: Thrust 的答案也是很重要的。建议再多做几个测试。
: In your code, the stream's state is tested first and then the string is read
: . in Thrust's code, the string is read first, and then the stream's state is
: tested. If the test is bad, the string is thrown away.
: You should do a few more tests, e.g. a string that ends with a whitespace (
: the delimiter), a string that contains a single word (no whitespace at all),
: etc., to make sure the code satisfies all of your needs.

avatar
m*l
11
是的,这是对的

【在 t****t 的大作中提到】
: 这个定式不是这样的, 应该这样写:
: while (iss>>piece) {
: oss<: }

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。