e*******s 发帖数: 1979 | 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究竟如何判断为空呢
有没有其他更好的办法 | k**********g 发帖数: 989 | 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();
| t****t 发帖数: 6806 | 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();
| e*******s 发帖数: 1979 | 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,
| e*******s 发帖数: 1979 | 5 many thanks!
【在 t****t 的大作中提到】 : 这个定式不是这样的, 应该这样写: : while (iss>>piece) { : oss<: }
| e*******s 发帖数: 1979 | 6 谢谢
请问一下和
while(iss){
iss>>piece;
oss<
}
的问题在哪里呢 iss>>piece 返回的也应该是&iss, 在末尾判定为false, 上面那段里
面是在下个iteration判断iss 但是似乎判定还是true
【在 t****t 的大作中提到】 : 这个定式不是这样的, 应该这样写: : while (iss>>piece) { : oss<: }
| e*******s 发帖数: 1979 | 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,
| k**********g 发帖数: 989 | 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,
| e*******s 发帖数: 1979 | 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.
| m*******l 发帖数: 12782 | 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.
| m*******l 发帖数: 12782 | 11 是的,这是对的
【在 t****t 的大作中提到】 : 这个定式不是这样的, 应该这样写: : while (iss>>piece) { : oss<: }
|
|