getline,stringstream,delimiter stuff

时间:2017-06-04 15:01:59

标签: delimiter cin getline stringstream

如何使用getline分隔字符串ex。 " you8only8live8once&#34 ;?这个解决方案只将我的第一个字设置为"你"然后在第一个" 8"停止。

如果我想要" 8"要成为一个任意的分隔符,我只需要输入cin>>分隔符;然后改变" getline(cin,短语,' 8');" to" getline(cin,phrase,delimiter);"?

istringstream inSS;
string phrase;
string firstWord;
string secondWord;
string thirdWord;
string fourthWord;

cout << "Please enter a digit infused string to explode:" << endl;
cout << "Please enter the digit delimiter:" << endl;

getline(cin, phrase, '8');

inSS.str(phrase);
inSS >> firstWord >> secondWord >> thirdWord >> fourthWord;


cout << "The 1st word is: " << firstWord << endl;
cout << "The 2nd word is: " << secondWord << endl;
cout << "The 3rd word is: " << thirdWord << endl;
cout << "The 4th word is: " << fourthWord << endl;

return 0;

0 个答案:

没有答案