stringstream默认分割空格、tab、回车换行
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s1, s2;
s1 = "Those who cannot remember the past are condemned to repeat it";
stringstream ss(s1);
while(ss >> s2)
cout << s2 << endl;
}
/*
输出:
Those
who
cannot
remember
the
past
are
condemned
to
repeat
it
*/
利用指定字符分割字符串
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s1, s2;
s1 = "Those*who*cannot*remember*the*past*are*condemned*to*repeat*it";
stringstream ss(s1);
while(getline(ss, s2, '*'))
cout << s2 << endl;
}
/*
输出:
Those
who
cannot
remember
the
past
are
condemned
to
repeat
it
*/