#include<iostream>
#include<sstream> //istringstream 必须包含这个头文件
#include<string>
using namespace std;
int main()
{
string str="123 456\n789";
istringstream is(str);
string s;
while(is>>s) // >> 按照字符流读入 所以是按' '或者'\n'分割
{
cout<<s<<endl;
}
}
输出是:
123
456
789
#include<iostream>
#include<sstream> //istringstream 必须包含这个头文件
#include<string>
using namespace std;
int main()
{
string str = "123/456/789";
istringstream is(str);
string s;
while(getline(is, s, '/')) // getline函数,自定义按照'/'分割
{
cout<<s<<endl;
}
return 0;
}
输出是:
123
456
789