问题:
一般对于一整行string句子,我们默认使用istringstram>>word
来根据空格来划分单词,但是我们如何自定义划分符来划分string句子呢?
解决方法:
使用getline来自定义字符来划分句子,来获得单词。
参考代码如下:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
string str1 = "test istringstream divide string with space";
string str2 = "test,istringstream,divide,with,other,char";
string word;
istringstream test1(str1), test2(str2);
while (test1>>word)
cout << word << ".";
cout << endl;
while (getline(test2, word, ','))//使用逗号来划分句子
cout << word << " ";
cout << endl;
system("pause");
return 0;
}
测试结果如下:
参考:
https://bbs.csdn.net/topics/360194050
https://bbs.csdn.net/topics/390667327