由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 问一段C++ iostringstream的代码
相关主题
how to read a sentence into a vector of string?(char **)返回值怎么用SWIG包成Python list (of strings
istream_iterator问题scala大牛幫看看這個map是為什麽?不太明白
这个有更好的算法吗?C++ 进阶问题
问一个关于convex set的数学问题 (转载)C++: operator new 为啥要是 static的, 不是有啥影响?
有没有类似strtok的函数[合集] Effective C++上说要少用casting,但是polymorphism一定要用到c
问一个python的string split问题[合集] 一些C++问题
怎样才能用perl等东西知道c macro中的数值[合集] three worst thing about C++(题目)
呼叫THRUST等C语言牛牛,菜鸟级C语言指针问题[合集] C++如何产生很大范围的随机数?
相关话题的讨论汇总
话题: string话题: iss话题: piece话题: std
进入Programming版参与讨论
1 (共1页)
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<: }

1 (共1页)
进入Programming版参与讨论
相关主题
[合集] C++如何产生很大范围的随机数?有没有类似strtok的函数
[合集] c++ 面试题一问问一个python的string split问题
[合集] C++的弱问题怎样才能用perl等东西知道c macro中的数值
[合集] C++ virtual function的一个问题。。呼叫THRUST等C语言牛牛,菜鸟级C语言指针问题
how to read a sentence into a vector of string?(char **)返回值怎么用SWIG包成Python list (of strings
istream_iterator问题scala大牛幫看看這個map是為什麽?不太明白
这个有更好的算法吗?C++ 进阶问题
问一个关于convex set的数学问题 (转载)C++: operator new 为啥要是 static的, 不是有啥影响?
相关话题的讨论汇总
话题: string话题: iss话题: piece话题: std