使用空格分隔符从输入流拆分字符串 [英] Splitting string from inputstream with whitespace delimiter
问题描述
我似乎无法用空白分隔符拆分我的字符串。
我试图使用getline(cin,myString)。
我输入的字符串为10 20 30,当我打印到输出流的字符串,我得到20 30,但不是10。 p>
我也试过了(cin >> string)。
/ div>
您可以使用 istringstream
:
;
getline(cin,str);
istringstream ss(str);
for(string word; ss>> word;)
cout<<单词<< endl;
$ c> while(cin>> str); 不会停止,除非:
-
-
发生EOF(您的电子邮件地址)。
>可以使用 Ctrl + Z , Ctrl + D , F6 系统)
-
用户定义的条件:
if(str.find('\\\
'))break;
I can't seem to split my string with the whitespace delimiter.
I tried using the getline(cin, myString).
I input the string as "10 20 30", when I print the string onto the output stream, I get "20 30", but not the 10.
I also tried the while(cin >> string). It works here, but the while loops never terminates.
Any help is appreciated.
You can use istringstream
:
string str;
getline(cin, str);
istringstream ss(str);
for(string word; ss >> word; )
cout << word << endl;
Your while(cin >> str);
doesn't stop unless:
Something fails in
>>
which is hard in your case.EOF occurs (You can use Ctrl+Z, Ctrl+D, F6 It depends on your system)
A user defined condition:
if(str.find('\n')) break;
这篇关于使用空格分隔符从输入流拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!